[HDU] 1160 FatMouse's Speed

本文提供了一种解决HDU 1160问题的方法,通过先对速度进行降序排序,然后寻找最长递增非连续子序列来确定最优解。文章详细介绍了实现过程及代码。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160

方法:建模后首先对速度降序排序,然后再排序后的结果序列中找一个最长的递增非连续子序列,并且记录前驱。

感想:虽然以来就根据速度排了序,但是要注意速度相等的情况。

代码:

View Code
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stack>
using namespace std;
int const MAX =0x3f3f3f3f;
struct Mouse
{
    int weight;
    int speed;
    int id;
    int pre;
};
bool cmp(Mouse x,Mouse y)
{
    if(x.speed > y.speed)
        return true;
    return false;
}
int main()
{
    Mouse mouses[1001];
    int dp[1001];
    memset(dp,0,sizeof(dp));
    int count=1;
    while(scanf("%d %d",&mouses[count].weight,&mouses[count].speed)!=EOF)
    {
        mouses[count].pre=0;
        mouses[count].id=count;
        count++;
    }
    sort(mouses+1,mouses+count,cmp);
    dp[1]=1;
    for(int i =2;i<count;i++)
    {
        int max = 0;
        for(int j=i-1;j>=1;j--)
        {
            if(mouses[j].weight <  mouses[i].weight && mouses[j].speed >  mouses[i].speed)
            {
                if(dp[j]>=max)
                {
                    max = dp[j];
                    mouses[i].pre = j;
                }
            }
        }
        dp[i] = 1+max;
    }
    int max = -MAX,end;
    for(int i=1;i<count;i++)
    {
        if(max <= dp[i])
        {
            max = dp[i];
            end = i;
        }
    }
    cout<<max<<endl;
    stack<int> st;
    int p=end;
    while(p!=0)
    {
        st.push(p);
        p=mouses[p].pre;
    }
    while(!st.empty())
    {
        cout<<mouses[st.top()].id<<endl;
        st.pop();
    }
    return 0;
} 

 

转载于:https://www.cnblogs.com/kbyd/archive/2013/05/02/3054195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值