zoukankan      html  css  js  c++  java
  • UVA 10131 Is Bigger Smarter?(DP)

    Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.

    The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.

    Say that the numbers on the i-th data line are W[i] and S[i]. 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 an elephant). If these n integers are a[1]a[2],..., a[n] then it must be the case that

       W[a[1]] < W[a[2]] < ... < W[a[n]]
    
    and
       S[a[1]] > S[a[2]] > ... > S[a[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 IQs 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<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    typedef long long LL;
    using namespace std;
    #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
    #define REP( i , n ) for ( int i = 0 ;2 i < n ; ++ i )
    #define CLEAR( a , x ) memset ( a , x , sizeof a )
    struct node{
        int id;
        int w,s;
    }e[1100];
    int dp[1100];
    int pre[1100];
    int ans=-1,pos;
    int cmp(node l1,node l2)
    {
        return l1.w<l2.w;
    }
    void print(int x)
    {
         if(pre[x]==-1)
         {
             printf("%d
    ",e[x].id);
             return ;
         }
         print(pre[x]);
         printf("%d
    ",e[x].id);
    }
    int main()
    {
        int num=1;
        while(~scanf("%d%d",&e[num].w,&e[num].s))
        {
            e[num].id=num;
            num++;
        }
        sort(e+1,e+num,cmp);
        ans=0;CLEAR(pre,-1);
        for(int i=1;i<=num;i++)
        {
            dp[i]=1;
            for(int j=1;j<i;j++)
            {
                if(e[i].w>e[j].w&&e[i].s<e[j].s&&dp[i]<dp[j]+1)
                {
                     dp[i]=dp[j]+1;
                     pre[i]=j;
                }
                if(dp[i]>ans)
                {
                    ans=dp[i];
                    pos=i;
                }
            }
        }
        printf("%d
    ",ans);
        print(pos);
        return 0;
    }
    


  • 相关阅读:
    jquery 实现弹出框 打开与关闭
    原生 将数组内容分别存入创建的循环单行栏(复选框+内容)里 并验证
    利用jquery 实现单页定位动画运动
    CSS样式 鼠标状态
    cookie 的使用:打开集团站自动跳转到当前城市所对应的网站,通过改变城市跳转到当前城市所对应的网站
    表单验证--通过原生js模仿ajax的异步交互
    springMVC之单文件上传
    springMVC之普通表单提交
    分页之页码数量显示
    cron表达式详解
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5220687.html
Copyright © 2011-2022 走看看