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;

    }

  • 相关阅读:
    HDU 3695 Computer Virus on Planet Pandora
    codeforces 706D Vasiliy's Multiset
    HDU 2222 Keywords Search
    POJ 2348 Euclid's Game
    HDU 1079 Calendar Game
    js选项卡的实现方法
    实现鼠标悬浮切换标题和内容
    js实现鼠标悬浮切换 setTab 代码实现
    自学Node.js: WebStorm+Node.js开发环境的配置
    windows 下安装nodejs
  • 原文地址:https://www.cnblogs.com/pandy/p/1322674.html
Copyright © 2011-2022 走看看