码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1160 FatMouse's Speed (最长上升子序列)

时间:2017-12-07 23:54:04      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:pre   www   mic   ret   mes   递归   names   printf   scan   

题目链接

题意:n个老鼠有各自的重量和速度,要求输出最长的重量依次严格递增,速度依次严格递减的序列,n最多1000,重量速度1-10000。

题解:按照重量递增排序,找出最长的速度下降子序列,记录序列每个位置的左边的位置,找到最大值终点再递归输出即可。(好久没做题了,花了很多时间才AC。)

#include <bits/stdc++.h>
using namespace std;
struct sa
{
    int weight,speed,pos,l;
} data[1005];
bool cmp(sa x,sa y)
{
    if(x.weight==y.weight) return x.speed>y.speed;
    return x.weight<y.weight;
}
int dp[1005];
void pr(int n)
{
    if(data[n].l) pr(data[n].l);
    printf("%d\n",data[n].pos);
}
int main()
{
    memset(data,0,sizeof(data));
    int n=1,ans=0;
    while(scanf("%d%d",&data[n].weight,&data[n].speed)!=EOF)
    {
        data[n].pos=n;
        data[n].l=0;
        n++;
    }
    sort(data+1,data+n+1,cmp);
    data[0].speed=99999999;
    memset(dp,0,sizeof(dp));
    for(int i=1; i<n; i++)
    {
        for(int j=0; j<i; j++)
        {
            if(data[j].weight<data[i].weight&&data[j].speed>data[i].speed&&dp[j]+1>dp[i])
            {
                dp[i]=dp[j]+1;
                data[i].l=j;
            }
            if(dp[i]>dp[ans])
                ans=i;
        }
    }
    //for(int i=0;i<=n;i++)
    //    printf("%d %d *\n",i,dp[i]);
    printf("%d\n",dp[ans]);
    pr(ans);
    return 0;
}

传送门:https://www.cnblogs.com/cenariusxz/p/4290837.html

HDU 1160 FatMouse's Speed (最长上升子序列)

标签:pre   www   mic   ret   mes   递归   names   printf   scan   

原文地址:http://www.cnblogs.com/Ritchie/p/8001324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!