zoukankan      html  css  js  c++  java
  • UVa 10131 Is Bigger Smarter?

    Question 1: Is Bigger Smarter?

    The Problem

    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

    基础的动态规划问题——最长上升子序列问题

    设dp[i]表示从A[0]到A[i]最长上升子序列的长度,可以得到如下状态转移关系

      dp[i]=max{ 0,dp[j] | j<i,A[j]<A[i] }+1

    这道题中,先把所有数据按weight值从小到大排序,然后在排好序的数据中按s求最大下降子序列

    设dp[i]是从dp[j]转移过来的,那么每次更新时记录 i 的前趋 j 即可在最后输出最长的序列

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 typedef struct
     9 {
    10     int num;
    11     int w;
    12     int s;
    13 } ELE;
    14 
    15 ELE e[1050];
    16 int dp[1050],Prev[1050];
    17 
    18 bool cmp(ELE a,ELE b)
    19 {
    20     return a.w<b.w||(a.w==b.w&&a.s>b.s);
    21 }
    22 
    23 int main()
    24 {
    25     int k=0,temp_w,temp_s;
    26     while(scanf("%d %d",&temp_w,&temp_s)==2)
    27     {
    28         e[k].num=k+1;
    29         e[k].w=temp_w;
    30         e[k].s=temp_s;
    31         k++;
    32     }
    33 
    34     sort(e,e+k,cmp);
    35 
    36     memset(dp,0,sizeof(dp));
    37     memset(Prev,-1,sizeof(Prev));
    38 
    39     for(int i=0;i<k;i++)
    40     {
    41         dp[i]=1;
    42         for(int j=i-1;j>=0;j--)
    43             if(e[i].w!=e[j].w&&e[i].s<e[j].s&&dp[i]<=dp[j]+1)
    44             {
    45                 dp[i]=dp[j]+1;
    46                 Prev[i]=j;
    47             }
    48     }
    49 
    50     int output[1050];
    51     int Start=0,t=0;
    52 
    53     for(int i=0;i<k;i++)
    54         if(dp[Start]<dp[i])
    55             Start=i;
    56 
    57     printf("%d
    ",dp[Start]);
    58 
    59     while(Start!=-1)
    60     {
    61         output[t++]=Start;
    62         Start=Prev[Start];
    63 
    64     }
    65 
    66     for(int i=t-1;i>=0;i--)
    67         printf("%d
    ",e[output[i]].num);
    68 
    69     return 0;
    70 }
    [C++]
  • 相关阅读:
    Jenkins构建、推送、拉取镜像和发布应用
    我们是如何做DevOps的?
    记录这两年是如何一步一步转型到.net core+k8s
    spring cloud+.net core搭建微服务架构:服务注册(一)
    .net core gRPC与IdentityServer4集成认证授权
    同时支持EF+Dapper的混合仓储,助你快速搭建数据访问层
    如何更优雅的在kubernetes平台下记录日志
    spring cloud+.net core搭建微服务架构:Api授权认证(六)
    spring cloud+.net core搭建微服务架构:配置中心续(五)
    spring cloud+.net core搭建微服务架构:配置中心(四)
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3554705.html
Copyright © 2011-2022 走看看