DP入门--HDU1160

本文介绍了一道最长上升子序列的变形题,并提供了一份详细的C语言实现代码。通过输入老鼠的体重和速度,利用动态规划思想找到满足条件的最大序列长度及具体序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

忙活了一个晚上终于完成了。。。

就是最长上升子序列的变形版。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
    int weight;
    int speed;
    int i;
    int mark;
    int father;
}node;
node mice[1002];
int cmp(const void *a,const void *b)
{
    return (*(node*)b).speed-(*(node*)a).speed;
}
int main()
{
    int t,k,h,tmp;
    int result[1001];
    int max,kmax;
    t=0;kmax=-100;
    while(scanf("%d%d",&mice[t].weight,&mice[t].speed)!=EOF)
    {
        mice[t].i=t+1;
        t++;
    }

    qsort(mice,t,sizeof(node),cmp);

    mice[0].mark=1;mice[0].father=0;
    for(k=1;k<t;k++)
    {
        max=-1;
        for(h=0;h<=k-1;h++)
        {
            if(mice[k].weight>mice[h].weight&&mice[h].mark>max)
            {
                max=mice[h].mark;
                mice[k].father=h;
            }
        }
        if(max>kmax)kmax=max;
        if(max==-1)
            mice[k].mark=1;
        else
            mice[k].mark=max+1;
    }


    kmax += 1;
    for(tmp=t-1;tmp>=0;tmp--)
        if(mice[tmp].mark==kmax)break;
    h=kmax;k=0;
    printf("%d\n",kmax);
    while(h--)
    {
        result[k]=mice[tmp].i;k++;
        tmp=mice[tmp].father;
    }
    for(h=kmax-1;h>=0;h--)
        printf("%d\n",result[h]);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值