本文主要是介绍UVa 10131 Is Bigger Smarter? (DPLIS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 number n; the remaining n lines should each contain a single positive integer (each one representing an elephant). If these n 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优先。
完整代码:
/*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;
}
这篇关于UVa 10131 Is Bigger Smarter? (DPLIS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!