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;
    }
  • 相关阅读:
    poj 1201 Intervals 差分约束系统
    poj 3237 Tree 树链剖分+线段树
    hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂
    hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
    hdu 5769 Substring 后缀数组 + KMP
    hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
    hdu 5690 2016"百度之星"
    hdu 5738 2016 Multi-University Training Contest 2 Eureka 计数问题(组合数学+STL)
    hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题
    hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并
  • 原文地址:https://www.cnblogs.com/a719525932/p/5764152.html
Copyright © 2011-2022 走看看