首页 > ACM题库 > HDU-杭电 > HDU 3617-Failure Is a Good Thing[解题报告]HOJ
2014
11-29

HDU 3617-Failure Is a Good Thing[解题报告]HOJ

Failure Is a Good Thing

问题描述 :


Last week, my granddaughter started kindergarten, and, as is conventional, I wished her success. I was lying. What I actually wish for her is failure. I believe in the power of failure.

Success is boring. Success is proving that you can do something that you already know you can do, or doing something correctly the first time, which can often be a problematical victory. First-time success is usually afluke. First-time failure, by contrast, is expected; it is the natural order of things.

Failure is how we learn. I have been told of an African phrase describing a good cook as "she who has broken many pots." If you’ve spent enough time in the kitchen to have broken a lot of pots, probably you know a fair amount about cooking. I once had a late dinner with a group of chefs, and they spent time comparing knife wounds and burn scars. They knew how much credibility their failures gave them.

– "Failure Is a Good Thing"

There are many kinds of failure, one of them is failing in exams. An exam has finished, and the score of each students has also been calculated. It is known that different students may get the same score.

Before the teacher telling his/her students their score, All the students are very nervous, so they get together to guess the rank of themselves in this example and the rank count backwards. Now the problem is: how many of the guesses will be correct at most?

There are n (1 ≤ n ≤ 1000) students participate in the exam, and the i-th student guesses: his/her rank is ri and is bi count backwards. (1 ≤ ri, bi ≤ n), different students may guess the same. A student’s rank is r means that there are r-1 students’s score is higher than him/her and the concept is similar when counts backwards.

输入:

The first line of the input file contains a signle integer: T (1 ≤ T ≤ 10), representing the number of test cases. For each test case, the first line contains a integer n, the second line has n integers: r1, r2, …, rn and in the third line there are n integers: b1, b2, …, bn.

输出:

The first line of the input file contains a signle integer: T (1 ≤ T ≤ 10), representing the number of test cases. For each test case, the first line contains a integer n, the second line has n integers: r1, r2, …, rn and in the third line there are n integers: b1, b2, …, bn.

样例输入:

1
3
3 1 3
1 3 3

样例输出:

2

#define maxn 1010
#include <iostream>
#include <cstdlib>

using namespace std;

int cor[maxn];
struct Tr{
	int a, b;
} r[maxn];

bool cmp(const Tr &a, const Tr &b){
	return (a.a<b.a || a.a==b.a&&a.b<b.b);
}

bool equ(const Tr &a, const Tr &b){
	return (a.a==b.a && a.b==b.b);
}

int n;

void init(){
	scanf("%d", &n);
	for (int i=0; i<n; i++) scanf("%d", &r[i].a);
	for (int i=0; i<n; i++) scanf("%d", &r[i].b);
	sort(r, r+n, cmp);
}

void solve(){
	int i=0, last=0; memset(cor, 0, sizeof cor);
	int ret = 0;
	while (i<n){
		int cnt=1;
		while (i+1!=n && equ(r[i], r[i+1])){
			i++; cnt++;
		}
		while (last!=r[i].a){
			last++;
			cor[last] = max(cor[last], cor[last-1]);
		}
		int tmp = (n+1 - (r[i].a+r[i].b) + 1);
		int maxcor = min(tmp, cnt);
		cor[last+tmp] = max(cor[last+tmp], cor[last] + maxcor);
		ret = max(ret, cor[last+tmp]);
		i++;
	}
	printf("%d\n", ret);
}

int main(int argc, char** argv) {
	int test;
	scanf("%d", &test);
	while (test--){
		init();
		solve();
	}
	return 0;
}

  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。

  2. 有一点问题。。后面动态规划的程序中
    int dp[n+1][W+1];
    会报错 提示表达式必须含有常量值。该怎么修改呢。。

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。