HDU 1160 FatMouse's Speed[LIS]

来源:互联网 发布:学汉语拼音的软件 编辑:程序博客网 时间:2024/05/22 00:12

题目:HDU 1160

题意:给出多排数据,猫的重量w,猫的速度s

weight1   speed1

weight2   speed2

......

要求找出最长的一组数据满足:

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

and

S[m[1]] > S[m[2]] > ... > S[m[n]]


Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
 

Sample Output
44597


代码:

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespace std;typedef long long LL;struct node{    int w;    int s;    int id;}m[1010];int cmp(node a,node b){    if(a.s==b.s)        return a.w>b.w;    return a.s<b.s;}int main(){    int cnt=0;    while(scanf("%d%d",&m[cnt].w,&m[cnt].s)!=EOF)    {        m[cnt].id=cnt+1;        cnt++;    }    sort(m,m+cnt,cmp);   //速度由小到大排序。    int f[1010]={0};    int pre[1010];    f[0]=1;    for(int i=1;i<cnt;i++)      //找出最长上升的体重    {        int maxx=0;        for(int j=0;j<i;j++)        {            if(m[i].w<m[j].w&&f[j]>maxx)            {                maxx=f[j];                pre[i]=j;      //前驱记录,输出路径。            }        }        f[i]=maxx+1;    }    int max_res=0;    int idx;    for(int i=0;i<cnt;i++)    {        if(f[i]>max_res)        {            max_res=f[i];            idx=i;        }    }    printf("%d\n%d\n",max_res,m[idx].id);    for(int i=1;i<max_res;i++)    {        printf("%d\n",m[pre[idx]].id);        idx=pre[idx];    }    return 0;}






0 0
原创粉丝点击