zoukankan      html  css  js  c++  java
  • hdu1160fatmouse.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;

    }

  • 相关阅读:
    数据结构一
    MVC5.0(一)
    异步多线程(六)lock锁
    异步多线程(五)多线程异常处理
    异步多线程(四)Task
    paypal payflow设置视频教程
    Java栈Stack知识点
    Java知识树梳理
    js定时器
    jdk环境变量配置改变不生效的问题
  • 原文地址:https://www.cnblogs.com/pandy/p/1322674.html
Copyright © 2011-2022 走看看