hdu 1160 FatMouse's Speed(最大上升子序列dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160


最大上升子序列的裸题,从给定样例中找到严格满足老鼠体重和速度成反比的尽量多的案例。先按照体重排序,然后在用dp求最大上升子序列(体重是降序排列的),求的时候要注意体重是严格单调的判断。


代码:

#include <bits/stdc++.h>
using namespace std;
struct node {
    int ind;
    int w,s;
    bool operator<(const node& other) const {
        if(w == other.w) {
            return s < other.s;
        }
        return w > other.w;
    }
};
node mouse[1005];
int dp[1005];
int pre[1005];
int main() {
    int n = 1;
    while(~scanf("%d%d", &mouse[n].w, &mouse[n].s)) {
        mouse[n].ind = n;
        n ++;
    }
    memset(pre, -1, sizeof(pre));
    sort (mouse + 1, mouse + n);
//    for (int i = 1; i < n; i ++) {
//        printf("%d ", mouse[i].ind);
//    }
//    printf("\n");
    int result = 0, resInd = -1;
    for (int i = 1; i < n; i ++) {
        dp[i] = 1;
        for (int j = 1; j < i; j ++) {
            if(mouse[i].s > mouse[j].s && mouse[i].w < mouse[j].w) {
                if(dp[i] < dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                    pre[i] = j;
                }
            }
        }
        if(dp[i] > result) {
            result = dp[i];
            resInd = i;
        }
    }
    printf("%d\n", result);
    while(pre[resInd] != -1) {
        printf("%d\n", mouse[resInd].ind);
        resInd = pre[resInd];
    }
    printf("%d\n", mouse[resInd].ind);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值