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;
    }
  • 相关阅读:
    游戏研发流程与构成要素
    Unity获取游戏对象详解
    Unity3D一些基本的概念和一些基本操作
    2019年Unity3D游戏开发前景预测及总结
    Unity3d游戏代码保护
    spring boot快速入门 6: 表单验证
    spring boot快速入门 5: 事务管理
    spring boot快速入门 4: jpa数据库操作 实现增删改查
    spring boot快速入门 3: controller的使用
    spring boot快速入门 2 :属性配置
  • 原文地址:https://www.cnblogs.com/a719525932/p/5764152.html
Copyright © 2011-2022 走看看