HDU1160 FatMouse's Speed(DP,最长下降子序列)

来源:互联网 发布:雅格迪斯号 淘宝 编辑:程序博客网 时间:2024/05/16 13:45

题目链接


FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7883    Accepted Submission(s): 3499
Special Judge


Problem Description
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 m[1], m[2],..., m[n] then it must be the case that 

W[m[1]] < W[m[2]] < ... < W[m[n]]

and 

S[m[1]] > S[m[2]] > ... > S[m[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
 

Source
Zhejiang University Training Contest 2001
 

先按W升序,然后对S求其最长下降子序列,直接套模板,注意“There may be many correct outputs for a given input, your program only needs to find one. ”,输出任意一个符合要求的结果即可


#include<stdio.h>#include<iostream>#include<stdlib.h>#include<algorithm>#include<time.h>#include<ctype.h>#include<string.h>#include<math.h>#include<string>#define sqr(x) (x)*(x)#define INF 0x1f1f1f1f#define PI 3.1415926535#define mm using namespace std;struct rat{int w,s,index;}r[1001];int dp[1001],pre[1001],temp[1001];int cmp(const void *x, const void *y){struct rat *a=(struct rat *)x;struct rat *b=(struct rat *)y;return a->w - b->w;}int main(){int cnt=0;while(~scanf("%d%d",&r[cnt].w,&r[cnt].s)){r[cnt++].index=cnt+1;}qsort(r,cnt,sizeof(struct rat),cmp);memset(dp,0,sizeof(dp));memset(pre,-1,sizeof(pre));memset(temp,0,sizeof(temp));dp[0]=1;int ans,k,end=0;for (int i=1;i<cnt;i++){ans=dp[i];for (int j=0;j<i;j++){if (r[i].s<r[j].s && dp[j]>=ans){ans=dp[j];pre[i]=j;}}dp[i]=ans+1;}ans=0;for (int i=0;i<cnt;i++){if (dp[i]>ans){ans=dp[i];end=i;}}k=end;temp[k]=1;while(k>=0){k=pre[k];temp[k]=1;}cout<<ans<<endl;for (int i=0;i<cnt;i++){if (temp[i])cout<<r[i].index<<endl;}return 0;}



1 0
原创粉丝点击