HDU 1160 FatMouse's Speed(最长递减子序列变形)

来源:互联网 发布:软件功能和技术特点 编辑:程序博客网 时间:2024/06/11 22:52

题目地址:点击打开链接

题意:给一堆老鼠,求一个最大的子集,子集的特点是从开头到结尾重量严格递增,速度严格递减

思路:先给所有老鼠按重量递增排个序,然后对速度求最长递减子序列

AC代码:

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;int dp[1001],pre[1001];struct mouse{    int weight,speed,num;}mou[1001];int cmp(const void *_a,const void *_b){    mouse *a = (mouse*)_a;    mouse *b = (mouse*)_b;    if(a->weight != b->weight)        return a->weight - b->weight;    return b->speed - a->speed;}void output(int i){    if(i == 0)        return;    output(pre[i]);    printf("%d\n",mou[i].num);}int main(){    int i,j,n = 1,max = 0,l;    while(scanf("%d%d",&mou[n].weight,&mou[n].speed) != EOF)    {        mou[n].num = n;        n++;    }    memset(dp,0,sizeof(dp));    memset(pre,0,sizeof(pre));    qsort(mou+1,n-1,sizeof(mouse),cmp);    dp[1] = 1;    for(i=2; i<=n; i++)    {        for(j=1; j<i; j++)        {            if(mou[i].weight > mou[j].weight && mou[i].speed < mou[j].speed && dp[i] < dp[j])//要严格递增,所以要再比较一次            {                dp[i] = dp[j];                pre[i] = j;            }        }        dp[i]++;    }    for(i=1; i<=n; i++)    {        if(dp[i] > max)        {            l = i;            max = dp[i];        }    }    printf("%d\n",max);    output(l);    return 0;}


0 0
原创粉丝点击