HDU1160 FatMouse's Speed(DP,最长递增子序列)

来源:互联网 发布:淘宝突然登不上去了 编辑:程序博客网 时间:2024/05/29 14:30

题目:

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15266    Accepted Submission(s): 6731
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
 

Recommend
Ignatius
 

Statistic | Submit | Discuss | Note
代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;struct node{    int x,y,pre;} a[10010];int dp[10010];bool cmp(node a,node b){    if(a.x!=b.x)        return a.x<b.x;    return a.y>b.y;}struct md{    int st,front;} b[10010];int flag=0;void shuchu(int i){    if(b[i].front)        shuchu(b[i].front);//递归输出    else    {        flag=1;        printf("%d\n",b[i].st);        return;    }    if(flag)    {        printf("%d\n",b[i].st);        return;    }}int main(){    int weight,speed,num=1;    while(~scanf("%d%d",&weight,&speed))//输入数据    {        a[num].x=weight;        a[num].y=speed;        a[num].pre=num;        num++;//以num作为下标,记录体重和速度的值    }    sort(a,a+num,cmp);//因为输出只要下标,所以这里可以打乱顺序,按体重从小到大排列    for(int i=0; i<num; i++)    {        dp[i]=1;//初始化为1,因为最少都有一个        b[i].st=a[i].pre;//用b来记录下标    }    for(int i=2; i<num; i++)    {        for(int j=i-1; j>=0; j--)            if(a[i].y<a[j].y&&a[i].x>a[j].x&&dp[i]<dp[j]+1)//求递增子序列,比较一下体重和素的的大小            {                dp[i]=dp[j]+1;//增加序列长度                b[i].st=a[i].pre;//记录下标                b[i].front=j;            }    }    int maxx=0,k;    for(int i=0; i<num; i++)        if(maxx<dp[i])        {            maxx=dp[i];//找出最长的            k=i;        }    printf("%d\n",maxx);    shuchu(b[k].front);//打印路径    printf("%d\n",b[k].st);//输出最后一个    return 0;}



0 0