HDU 1160 FatMouse's Speed(LIS+记录路径)

来源:互联网 发布:信息碎片化定义知乎 编辑:程序博客网 时间:2024/06/05 10:21

题目来源:HDU 1160

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
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

     心路历程:第一反应是对一个指标排序,另一个指标dp求lis,最后卡在了如何输出路径(没学学长代码的后果)
                     此题需要注意的问题:注意在对另一个没排序的指标求lis时,要加一句前一个指标是否相等,因为题目
                     说了严格大于。然后就是熟悉一下如何输出路径,单独开一个数组来保存路径,随输出随更新。
   ac代码如下:
   
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <string.h>using namespace std;struct mouse{    int w,s,index;//分别保存了重量速度和初始角标}m[1111];bool cmp(struct mouse a,struct mouse b)//按照重量先从小到大排个序{    return a.w<b.w;}int main(){    int w,s,n,i=0,j,dp[1111],k,brr[1111],pre[1111],max=1;//pre[]用来保存路径,max注意初始化是1    while(scanf("%d%d",&w,&s)!=EOF)//题目输入比较诡异    {        i++;        m[i].w=w,m[i].s=s;        m[i].index=i;        dp[i]=1;        pre[i]=-1;//对上一个路径的初始化    }    k=i;    int answer=k;    sort(m+1,m+1+k,cmp);    for(i=k;i>=1;i--)//反向对速度求最大上升子序列    {        for(j=k;j>i;j--)        {            if(m[i].s>m[j].s&&m[i].w<m[j].w&&dp[i]<dp[j]+1)//注意前一个指标不能相等            {                dp[i]=dp[j]+1;                pre[i]=j;//保存角标            }        }        if(max<dp[i])        {            max=dp[i];            answer=i;//保存最后的角标        }    }    printf("%d\n",max);    do//输出路径,由于我是反向求最大上升子序列,所以输出直接输出,否则的话还要将路径逆向输出。    {        printf("%d\n",m[answer].index);        answer=pre[answer];    }while(answer!=-1);    return 0;}

期末不挂!


原创粉丝点击