Is Bigger Smarter?(最长上升子序列)

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to analyze a collection of elephants and place as large a subset of elephants as possible into a sequence whose weights are increasing but IQ’s are decreasing.
The input will consist of data for a bunch of elephants, at one elephant per line terminated by the end-of-file. The data for each 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 10,000. The data contains information on at most 1,000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.
The first output line should contain an integer n, the length of elephant sequence found. The remaining n lines should each contain a single positive integer representing an elephant. Denote the numbers on the ith data line as W[i] and S[i]. If these sequence of n elephants 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 must be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. Your program can report any correct answer for a given input.
Sample Input6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output4
4
5
9
7

HINTSourceWaterloo Local Contest Oct. 7, 1997
有一堆大象,如果大象越重,iq越小就符合题意,找出最多数量的大象,并输出他们的任意一种结果
思路:用结构体存储,对iq降序,再找他们的体重的最长上升子序列,DP思路

假如一个序列是 1 5 4 8 9 6 2 3 4 5 6
dp数组的值为 1 2 2 3 4 3 2 3 4 5 6
dp的意义是,这个位置的这个数存在于这个序列里的最长上升子序列

代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e6;
using namespace std;
struct node
{
    int a;
    int b;
    int num;
} nod[1005];
int dp[1005];
int ne[1005];
int o[1005];
bool cmp(node a,node b)
{
    return a.b>b.b;
}
int main()
{
     ios::sync_with_stdio(false);
     int d=0;
     while(scanf("%d%d",&nod[d].a,&nod[d].b)!=EOF)
       nod[d].num=d+1,dp[d]=1,d++;
 
//    int d;
//    cin>>d;
//    for(int i=0; i<d; i++)
//        dp[i]=1,nod[i].num=i+1,scanf("%d%d",&nod[i].a,&nod[i].b);
    sort(nod,nod+d,cmp);
    for(int i=0; i<d; i++)
        for(int j=0; j<i; j++)
        {
            if(nod[i].a>nod[j].a)
            {
                dp[i]=max(dp[i],dp[j]+1);
                if(dp[i]==dp[j]+1)
                    ne[i]=j;
            }
        }
    int ans=0,maxx;
    for(int i=0;i<d;i++)
    {
        if(dp[i]>ans)
            ans=dp[i],maxx=i;
    }
    cout<<ans<<endl;
 
    int i,j=0;
    for(i=maxx;ans!=0;i=ne[i])
    {
        o[++j]=nod[i].num;
        --ans;
    }
    for(int k=j;k>=1;k--)
        cout<<o[k]<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值