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;
    }
  • 相关阅读:
    [Windows] 一些简单的CMD命令
    开发过程中用到的触发器
    MyEclipse8.5配置struts等框架
    Java编程中中文乱码问题的研究及解决方案
    开源的SSH框架优缺点分析
    java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序
    html,CSS文字大小单位px、em、pt的关系换算
    HTML常用标签参考学习
    匹配中文字符的正则表达式
    Oracle 取上周一到周末的sql
  • 原文地址:https://www.cnblogs.com/a719525932/p/5764152.html
Copyright © 2011-2022 走看看