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

【HDU】 1160 FatMouse's Speed (DP)

时间:2015-05-17 12:20:00      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

一开始写的dfs进行记忆化结果不知道怎么进行路径的记录。。。改成循环就好了

dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i]

一开始进行一次排序使得重量递增,这样只需要考虑速度就好了

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10005;
struct Mouse{
    int w,s,id;
    friend bool operator < (Mouse p,Mouse q){
        return p.w < q.w;
    }
}mouse[maxn];
int cnt = 1;
int dp[maxn];
int pre[maxn];
void print_path(int pos){
    if(pos == 0) return;
    print_path(pre[pos]);
    printf("%d\n",pos);
}
void debug(){
    for(int i = 1; i < cnt; i++)
        printf("(%d %d %d)\n",mouse[i].w,mouse[i].s,mouse[i].id);
}
int main(){
    while(scanf("%d%d",&mouse[cnt].w,&mouse[cnt].s) != EOF){
        //if(mouse[cnt].w == 0)   break;
        dp[cnt] = 1;
        mouse[cnt].id = cnt;
        pre[cnt] = 0;
        cnt ++;
    }
    sort(mouse + 1,mouse + cnt);
    //debug();
    int ans = 0,start = 0;
    for(int i = 1; i < cnt; i++){
        for(int j = 1; j < i; j++){
            if(mouse[j].w < mouse[i].w && mouse[j].s > mouse[i].s){
                if(dp[j] + 1 > dp[i]){
                    dp[i] = dp[j] + 1;
                    pre[mouse[i].id] = mouse[j].id;
                }
            }
        }
        if(ans < dp[i]){
            start = mouse[i].id;
            ans = dp[i];
        }
    }
    printf("%d\n",ans);
    print_path(start);
    return 0;
}
/*
1 2
3 4
5 6
7 9
9 10
3 8
*/

【HDU】 1160 FatMouse's Speed (DP)

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/45787413

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