What is a bigger smarter? (Longest ascending sub-sequence of DP)

Source: Internet
Author: User

Description

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 participating 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 in 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 areW [I]AndS [I]. Your program shocould output a sequence of lines of data; the first line shoshould contain a numberN; The remainingNLines shoshould each contain a single positive integer (each one representing an elephant). If theseNIntegers areA [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, NShocould be as large as possible. all inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. there may be required correct outputs for a given input, your program only needs to find one.

 

Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample output
44597
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6  7 const int maxn=1005; 8 int d[maxn],pre[maxn],n; 9 10 struct node11 {12     int x,y,id;13 }p[maxn];14 15 bool mycomp(node a,node b)16 {17     if(a.x!=b.x) return a.x<b.x;18     return a.y>b.y;19 }20 21 void printf_ans(int n,int i)22 {23     if(n==0) return ;24     int u=pre[i];25     printf_ans(n-1,u);26     printf("%d\n",p[i].id);27 }28 29 bool judge(int j,int i)30 {31     if(p[j].x<p[i].x&&p[j].y>p[i].y)32         return true;33     return false;34 }35 36 int main()37 {38     int i,j;n=1;39     while(~scanf("%d%d",&p[n].x,&p[n].y))40     {41         p[n].id=n;n++;42     }43     n--;44     sort(p+1,p+n+1,mycomp);45     memset(pre,-1,sizeof(pre));46     memset(d,0,sizeof(d));47     p[0].x=-100000;p[0].y=100000;48     for(i=1;i<=n;i++)49     {50         for(j=0;j<i;j++)51         {52             if(judge(j,i) && d[i]<d[j]+1)53             {54                 d[i]=d[j]+1;pre[i]=j;55             }56         }57     }58     int ansm,ansi;59     for(i=1;i<=n;i++)60     {61         if(ansm<d[i])62         {63             ansm=d[i];ansi=i;64         }65     }66     printf("%d\n",ansm);67     printf_ans(ansm,ansi);68     return 0;69 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.