码迷,mamicode.com
首页 > 其他好文 > 详细

[FatMouse's Speed] LIS

时间:2020-03-30 19:29:07      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:lang   this   lis   speed   eof   fas   print   cti   col   

Description

HDU-1160
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

Solution

先按照重量排序,再求出最长上升子序列,记录模板在此。

Code

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define max(a,b) a>b?a:b
#define inf 0x3f3f3f3f
int dp[1050];

struct Mouse{
    int w,s,n,l;
}m[1050];

int cmp(Mouse m1,Mouse m2){
    if(m1.w==m2.w)return m1.s<m2.s;
    return m1.w>m2.w;
}

int main(){
    int c=1,i,j;
    while(scanf("%d%d",&m[c].w,&m[c].s)!=EOF){
        m[c].n=c;
        m[c].l=0;
        c++;
    }
    sort(m+1,m+c+1,cmp);
    m[0].w=inf;
    m[0].s=0;
    m[0].l=0;
    m[0].n=0;
    int ans=0;
    for(i=1;i<=c;i++){
        dp[i]=0;
        for(j=0;j<i;j++){
            if(m[j].w>m[i].w&&m[j].s<m[i].s){
                if(dp[j]+1>=dp[i]){
                    dp[i]=dp[j]+1;
                    m[i].l=j;
                    if(dp[i]>dp[ans])ans=i;
                }
            }
        }
    }
    printf("%d\n",dp[ans]);
    while(m[ans].l!=0){
        printf("%d\n",m[ans].n);
        ans=m[ans].l;
    }
    printf("%d\n",m[ans].n);
    return 0;
}

[FatMouse's Speed] LIS

标签:lang   this   lis   speed   eof   fas   print   cti   col   

原文地址:https://www.cnblogs.com/ez4zzw/p/12600266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!