【dp每日一题】HDU 1160 FatMouse's Speed

大意:

给出n个老鼠的体重x和速度y,要求找出最多的一组老鼠,使他们严格符合体重上升,速度下降

思路:

先按照体重排一下序,然后求最长下降子序列即可,不过需要记录路径,开一个pre数组即可

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 5;
typedef long long LL;
int cnt, dp[N], res, pre[N];
struct node {
    int x, y, id;
} a[N];
bool cmp(node a, node b) {
    if (a.x == b.x) return a.y > b.y;
    return a.x < b.x;
}
int main() {
    while (scanf("%d%d", &a[cnt].x, &a[cnt].y) != EOF) {
        dp[cnt] = 1;
        pre[cnt] = -1;
        cnt++;
        a[cnt-1].id = cnt;
    }
    int ed = 0;
    sort(a, a + cnt, cmp);
    for (int i = 0; i < cnt; i++) {
        for (int j = 0; j < i; j++) {
            if (a[j].y > a[i].y&&a[i].x>a[j].x) {
                if (dp[j] + 1 > dp[i]){
                    pre[i] = j;
                    dp[i] = dp[j] + 1;
                } 
            }
        }
        if (dp[i] > res) {
            res = dp[i];
            ed = i;
        }
    }
    cout << res << endl;
    stack<int> s;
    while (ed != -1) {
        s.push(a[ed].id);
        ed = pre[ed];
    }
    while(!s.empty()){
        cout << s.top() << endl;
        s.pop();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/14194191.html