zoukankan      html  css  js  c++  java
  • [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;
    } 
  • 相关阅读:
    python opencv PyQt5
    各大web服务器https的证书文件
    mysql 常用字符串操作
    python 修改字符串中的某一位字符
    python mysql
    小程序
    m4a 转MP3
    安装python 3.7
    树莓派版本信息
    bash 重启后台程序脚本
  • 原文地址:https://www.cnblogs.com/kbyd/p/3054195.html
Copyright © 2011-2022 走看看