[HDU] 1160 FatMouse's Speed

题目链接: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/p/3054195.html