10131 - Is Bigger Smarter?
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1072
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.
The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.
Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a numbern; the remaining n lines should each contain a single positive integer (each one representing an elephant). If thesen integers are a[1], a[2],..., a[n] then it must be the case that
W[a[1]] < W[a[2]] < ... < W[a[n]]
and
S[a[1]] > S[a[2]] > ... > S[a[n]]
In order for the answer to be correct, n should be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
Sample Output
4 4 5 9 7
怎么排序?可以按照first和second均从大到小排,first优先。
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
完整代码:
/*0.016s*/ #include<bits/stdc++.h> using namespace std; const int MAXN = 1001; struct node { int wei, iq, id; bool operator < (const node& a) const { return wei > a.wei || wei == a.wei && iq > a.iq; ///iq也这么排是为了方便后面求dp时不用判断wei是否相等~ } } e[MAXN]; int dp[MAXN], path[MAXN]; void print(int x) { printf("%d\n", e[x].id); if (path[x]) print(path[x]); else printf("%d\n", e[0].id); } int main() { int n = 0, ans = 0, i, j; while (~scanf("%d%d", &e[n].wei, &e[n].iq)) e[n].id = n + 1, dp[n++] = 1; sort(e, e + n); for (i = 1; i < n; ++i) for (j = 0; j < i; ++j) if (e[j].iq < e[i].iq && dp[j] + 1 > dp[i]) path[i] = j, dp[i] = dp[j] + 1; for (i = 1; i < n; ++i) if (dp[ans] < dp[i]) ans = i; printf("%d\n", dp[ans]); print(ans); return 0; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索for
, data
, and
, The
should
10131 uva、en10131是什么材料、en10131、乐高10131、en10131标准,以便于您获取更多的相关知识。