ACM--steps--dp--3.2.3--FatMouse's Speed

来源:互联网 发布:spring 启动加载数据 编辑:程序博客网 时间:2024/05/17 21:42

FatMouse's Speed

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

基础还是太薄弱了一些,参考着别人的代码才写出来的。
http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127944.html
状态转移方程为f[i] = max{f[j], mice[j].speed > mice[i].speed} + 1, 最后找出最大的f[i]即可

#include<iostream>#include<algorithm>using namespace std;const int N=1020;struct dyx{    int wei,spe;//表示重量以及速度。    int num;//表示当前的标号}mouse[N];int dp[N];//用以寻找最大子列的长度。dp[i]表示符合条件的以第i个数据结尾的最长子列长度。int wyx[N];//表示当前值的上一个值的序号,用以输出序列。/*先理清一下思路老鼠的体重递增,而速度要递减。然后要输出最大子列的长度,和他们序列。*/bool cmp(dyx a,dyx b){    if(a.wei<b.wei)    return 1;//体重按照升序排列、    else if(a.wei==b.wei&&a.spe>a.spe)    return 1;    else    return 0;}int main(){    int i=1,j;    while(cin>>mouse[i].wei>>mouse[i].spe)    {        dp[i]=1;        wyx[i]=0;        mouse[i].num=i;        i++;    }    int n=i-1;    sort(mouse+1,mouse+1+n,cmp);//已给定的方式进行排序。    int max_len=0;//表示最大子列的长度、    int max_num=0;//表示最大子列的最后一个数字的标号。    for(i=1;i<=n;i++)    {        for(j=1;j<i;j++)        {            if(mouse[i].wei>mouse[j].wei&&mouse[i].spe<mouse[j].spe&&dp[i]<dp[j]+1)            {                dp[i]=dp[j]+1;                wyx[i]=j;            }            if(dp[i]>max_len)            {                max_num=i;                max_len=dp[i];            }        }    }    //准备输出序列的标号。    int t=max_num;    i=0;    int fin[N];//最终序列的标号。    while(t)    {        fin[i++]=t;        t=wyx[t];    }    cout<<i<<endl;    while(i>0)    {        i--;        cout<<mouse[fin[i]].num<<endl;    }    return 0;}



0 0