zoukankan      html  css  js  c++  java
  • uva10131Is Bigger Smarter? (dp DAG最长路)

    题目大意是给出 大象的体重和智商,要求选出最多的大象能组成 体重严格增大,智商严格减小

    是《入门经典》上的DAG 嵌套矩形

    d[i] = max ( d[j] +1 )   i,j 为相连的边

    题目:

    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
    


    代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <memory.h>
     5 using namespace std;
     6 
     7 int weight[1005];
     8 int IQ[1005];
     9 int num;
    10 int G[1005][1005];
    11 
    12 int d[1005];
    13 int dp(int i)
    14 {
    15     int &ans = d[i];
    16 
    17     if(ans>0)return ans;
    18 
    19     ans =1;
    20     for(int j=0 ; j<num;j++)
    21     {
    22         if(i!=j && G[i][j]) ans = max ( ans, dp(j)+1);
    23     }
    24     return ans;
    25 }
    26 void print_ans(int i)
    27 {
    28     cout<<i+1<<endl;
    29     for(int j=0;j<num;j++)
    30     {
    31         if(j!=i && G[i][j] && d[i]== d[j]+1)
    32         {
    33           
    34             print_ans(j);
    35             break;
    36         }
    37     }
    38 }
    39 
    40 int main()
    41 {
    42     freopen("IN","r",stdin);
    43 
    44     while(cin>>weight[num]>>IQ[num])
    45     {
    46         num++;
    47     }
    48 
    49 
    50     memset(d,0,sizeof(d));
    51     for(int i=0;i<num;i++)
    52     {
    53         for(int j=0;j<num;j++)
    54         {
    55             if(i!=j &&  weight[i]< weight[j] && IQ[i]>IQ[j])
    56             {
    57                 //cout<<weight[i]-weight[j] << "  "<<IQ[i]-IQ[j]<<" "<<i<< " "<<j<<endl;
    58                 G[i][j]=1;
    59             }
    60         }
    61     }
    62    // cout<<weight[6]<<" "<<weight[7] <<" "<<IQ[6] << IQ[7] <<endl;
    63     for(int i=0;i<num;i++)
    64     {
    65       //  cout<<" I "<<i <<" "<<d[i]<<endl;
    66         d[i] = dp(i);
    67         //cout<<d[i]<<" I "<<i<<endl;
    68       //  cout<<" I "<<i <<" "<<d[i]<<endl;
    69     }
    70 
    71     int maxn=d[0];
    72     int I=0;
    73     for(int i=0;i<num;i++)
    74     {
    75         if(d[i]>=maxn)
    76         {
    77             maxn=d[i];
    78             I=i;
    79         }
    80     }
    81     cout<<maxn<<endl;
    82     print_ans(I);
    83 
    84 
    85     return 0;
    86 }
  • 相关阅读:
    二十七、正则表达式补充
    二十六、python中json学习
    二十五、python中pickle序列学习(仅python语言中有)
    MongoDB系列
    产品经理思考
    摩拜数据产品
    龙珠直播之swot
    ahp层次分析法软件
    用户画像之门店用户类型的体系
    汽车后市场SWOT分析
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3496765.html
Copyright © 2011-2022 走看看