HDU

来源:互联网 发布:python kgram算法 编辑:程序博客网 时间:2024/06/08 08:12

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing. 
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file. 

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice. 

Two mice may have the same weight, the same speed, or even the same weight and speed. 
Output
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 a mouse). If these n integers are m11, m22,..., mnnthen it must be the case that 

Wm[1m[1] < Wm[2m[2] < ... < Wm[nm[n

and 

Sm[1m[1] > Sm[2m[2] > ... > Sm[nm[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 speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 
Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample Output
44597


#include<cstdio>#include<cstring>#include<algorithm>#define max_n 2010typedef long long LL;using namespace std; int dp[max_n],v[max_n];struct node{int a;int b;int c;int d;}s[max_n];bool cmp1(node x,node y){ return x.a>y.a;}int main(){int n,m,u=1;while(scanf("%d %d",&n,&m)!=EOF){s[u].a=n;s[u].b=m;s[u].c=u;s[u].d=0;u++;}sort(s+1,s+u,cmp1);s[0].a=0x3f3f3f3f;s[0].b=0;s[0].c=0;s[0].d=0;int ans=0;for(int i=1;i<u;i++){dp[i]=0;for(int j=0;j<i;j++){if(s[i].a<s[j].a && s[i].b>s[j].b){if(dp[i]<=dp[j]+1){dp[i]=dp[j]+1;s[i].d=j;if(dp[i]>dp[ans]) ans=i;}}}}printf("%d\n",dp[ans]);while(s[ans].d!=0){printf("%d\n",s[ans].c);ans=s[ans].d;}printf("%d\n",s[ans].c);return 0;}

原创粉丝点击