zoukankan      html  css  js  c++  java
  • HDU 1160 dp中的路径问题

    Description

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing. 

    Input

    Input contains data for a bunch of mice, one mouse per line, terminated by end of file. 

    The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice. 

    Two mice may have the same weight, the same speed, or even the same weight and speed. 

    Output

    Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that 

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

    and 

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

    In order for the answer to be correct, n should be as large as possible. 
    All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 

    Sample Input

    6008 1300
    6000 2100
    500 2000
    1000 4000
    1100 3000
    6000 2000
    8000 1400
    6000 1200
    2000 1900

    Sample Output

    4
    4
    5
    9
    7

    按照体重递增 速度递减的方式排序 然后找到最大的串 主要是路径问题


    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    using namespace std;
    #define maxn 10005
    #define N 3000
    int dp[N],p[N],a[N];
    struct node
    {
        int w,s,t;
    }q[N];
    int cmp(node e,node f)
    {
        if(e.w!=f.w) return e.w<f.w;
        return e.s>f.s;
    }
    int main()
    {
        int n=1;
        while(scanf("%d%d",&q[n].w,&q[n].s)!=EOF)
        {
            q[n].t=n;
            dp[n]=1;
            p[n]=0;
            n++;
        }
        n=n-1;
        sort(q+1,q+1+n,cmp);
        int maxx=-12365478,m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                if(q[i].w>q[j].w&&q[i].s<q[j].s&&dp[i]<=dp[j])///dp[j]>=dp[i] 即dp[i]=dp[j]+1存进去才有意义
                {
                    dp[i]=dp[j]+1;
                    p[i]=j;///p[i]=j保留上一个的位置  方便依次查找
                    if(dp[i]>=maxx)
                    {
                        maxx=dp[i];
                        m=i;///保留最大值的位置
                    }
                }
            }
        }
        int x=m,i=0;
        while(x)
        {
            a[i++]=x;///提取到数组
            x=p[x];///依次查找
        }
        printf("%d
    ",i);
        while(i>0)
        {
            i--;
            printf("%d
    ",q[a[i]].t);///输出
        }
        return 0;
    }
  • 相关阅读:
    将Excel文件转换为Html
    怎样录制屏幕并将结果保存为Gif
    Spire.Pdf 的各种操作总结
    在C#中使用Spire.doc对word的操作总结
    使用Spire.Barcode程序库生成二维码
    【BZOJ1304】[CQOI2009]叶子的染色(动态规划)
    【BZOJ1303】[CQOI2009]中位数图(模拟)
    【BZOJ1297】[SCOI2009]迷路(矩阵快速幂)
    【BZOJ1296】[SCOI2009]粉刷匠(动态规划)
    【BZOJ1295】[SCOI2009]最长距离(最短路)
  • 原文地址:https://www.cnblogs.com/a719525932/p/5764152.html
Copyright © 2011-2022 走看看