您的位置:首页 > 其它

UVA - 10131 Is Bigger Smarter?

2013-09-09 23:03 417 查看
题意:求最长上升子序列,因为题目是从一群大象中尽量多选出大象使得,体重递增,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: