UVA - 10131 Is Bigger Smarter?

题意:求最长上升子序列,因为题目是从一群大象中尽量多选出大象使得,体重递增,IQ递减,我们可以事先排序 ,接着就是在求最长上升子序列的同时,注意IQ递减,然后再循环一次输出选择的大象标号

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1010;

struct eleph 
{
        int n,w,s;
}arr[MAXN],res[MAXN];
int T[MAXN];

bool cmp(const eleph& x , const eleph& y)
{
        if (x.w < y.w) 
            return true;
        else if(x.w == y.w && x.s > y.s) 
            return true;
        return false;
}

int main()
{
    int num=0;
    int ans=0;
    int wei,iq;
    while(scanf("%d%d",&wei,&iq) != EOF)
    {
        num++;
        arr[num].n = num;
        arr[num].w = wei;
        arr[num].s = iq;
    }
    sort(arr+1,arr+num+1,cmp);
    arr[0].s=10010;
    int index;
    for(int i = 1; i <= num; i++)
    {
        for(int j = 0; j < i; j++)
            if(arr[i].s < arr[j].s && arr[i].w > arr[j].w)
                T[i] = max(T[i],T[j]+1);
        if(T[i] > ans)
        {
            ans = T[i];
            index = i;
        }   
    }
    printf("%d\n",ans);
    int sz = ans;
    res[ans--] = arr[index];
    for(int i = index - 1; i > 0 && ans; i--)
        if(T[i] == ans && res[ans+1].s < arr[i].s && res[ans+1].w > arr[i].w)
            res[ans--] = arr[i];
    for(int i = 1; i <= sz; i++)
        printf("%d\n",res[i].n);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值