hdu1160--fatmouse.speed

动态规划题,麻烦在要输出一个最优解

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<stack>
struct tmice{
    int weight;
    int speed;
    int id;
}mice[1001];
int f[1001],r[1001],b[1001];
int compare( const void * a,  const void *b)
{
    tmice * a1=(tmice *)a;tmice * b1=(tmice *)b;
    if(a1->weight!=b1->weight)
        return a1->weight-b1->weight;
    else
        return b1->speed-a1->speed;
}
int main()
{
    int i,count=0,j,max=-1000,maxid=0;
    while(scanf("%d %d",&mice[count].weight,&mice[count].speed)!=EOF)
    {
        mice[count].id=count+1;
        count++;
    }
    qsort(mice,count,sizeof(mice[0]),compare);
    for(i=0;i<count;i++)
    {
        f[i]=1;r[i]=-1;
        for(j=i-1;j>=0;j--)
        {
            if(mice[i].weight>mice[j].weight&&mice[i].speed<mice[j].speed&&f[j]+1>f[i])
            {
                f[i]=f[j]+1;r[i]=j;
            }
        }
        if(f[i]>max)
        {
            max=f[i];maxid=i;
        }
    }
    printf("%d\n",max);i=0;
    while(maxid!=-1)
    {
        b[i]=maxid;maxid=r[maxid];i++;
    }
    i--;
    while(i>=0)
    {
        maxid=b[i];i--;
        printf("%d\n",mice[maxid].id);

    }
    return 0;

}

你可能感兴趣的:(HDU)