zoukankan      html  css  js  c++  java
  • 动态规划(DP),类似LIS,FatMouse's Speed

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108

    解题报告:

    1、首先按照weight从小到大排列,weight相同的按照speed从大到小排列;

    2、Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度;

    3、path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置

    4、递归输出id

    void output(int path[],pos)
    {
        if(pos==0) return ;
        output(path,path[pos]);
        printf("%d
    ",mice[pos].id);
    }

    Sources Code:

    #include <cstdio>
    #include <stdlib.h>
    #include <algorithm>
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    int n;///n只老鼠
    int Count[1001]= {0}; ///count[i]表示构造到第i个老鼠时,序列的最大长度
    int path[1001]= {0}; ///path[i]表示构造到第i个老鼠时的前一个老鼠(前驱)
    
    struct mouse
    {
        int weight;
        int speed;
        int id;
    } mice[1001];
    
    int cmp(const void *a,const void *b)
    {
        struct mouse *p1=(mouse *)a;
        struct mouse *p2=(mouse *)b;
        if(p1->weight==p2->weight)
        {
            if(p1->speed>p2->speed)
                return p2->speed-p1->speed;
            else return p1->speed-p2->speed;
        }
        else return p1->weight-p2->weight;
    }
    
    void output(int path[],int pos)
    {
        if(pos==0) return ;
        output(path,path[pos]);
        printf("%d
    ",mice[pos].id);
    }
    
    int main()
    {
        n=0;
        int i=0,j=0;
        while(scanf("%d%d",&mice[++i].weight,&mice[++j].speed)!=EOF)
        {
            n++;
            mice[n].id=n;
        }
        qsort(mice+1,n,sizeof(mice[0]),cmp);
        Count[1]=1;
        for(int i=2; i<=n; i++)
        {
            for(int j=1; j<i; j++)
            {
                if(mice[i].weight>mice[j].weight&&mice[i].speed<mice[j].speed)
                {
                    if(Count[i]<Count[j])
                    {
                        Count[i]=Count[j];
                        path[i]=j;
                    }
                }
            }
            Count[i]++;
        }
        int _max=0;
        int pos;
        for(int i=1; i<=n; i++)
        {
            if(Count[i]>_max)
            {
                _max=Count[i];
                pos=i;
            }
        }
        printf("%d
    ",_max);
        output(path,pos);
    }
  • 相关阅读:
    openstack running 2
    openstack running 3
    好东西哟 XD
    Linux 上課用細部調整(转)
    openstack swift install 1
    Spring初识(通过小实例清晰认识Spring)
    Windowphone中如何将项目导出为模板
    WP8点击桌面图标快速恢复应用
    WindowsPhone8中SaveSong方法将音乐文件转存到音乐库中
    Windows Phone 数据绑定之UI Element Binding
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5263661.html
Copyright © 2011-2022 走看看