HDU

来源:互联网 发布:nginx 禁止某ip访问 编辑:程序博客网 时间:2024/05/22 03:11

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

题意:给你一堆重量和速度,要你求重量越来越重,速度越来越慢的最多个数并输出

思路:就是一个两个条件的LIS和打印路径,先按重量排一下序,然后LIS,用p数组记录前驱,然后打印就行了


代码:

#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;typedef long long LL;typedef pair<LL, LL> P;const int maxn = 1e3 + 5;const int mod = 1e8 + 7;struct node {    int w;    int s;    int key;} a[maxn];bool cmp(node a, node b) {    if (a.w < b.w) return 1;    else if (a.w == b.w && a.s > b.s) return 1;    else return 0;}int p[maxn], dp[maxn], ans[maxn];int main() {    int num = 0;    memset(p, -1, sizeof(p));    memset(dp, 0, sizeof(dp));    while (~scanf ("%d%d", &a[num].w, &a[num].s)) {        a[num].key = num + 1;        p[num] = 0;        dp[num] = 1;        num++;    }    sort(a, a + num, cmp);    int Max = -INF, Maxi;    for (int i = 0; i < num; i++) {        for (int j = 0; j < i; j++) {            if (a[i].w > a[j].w && a[i].s < a[j].s && dp[j] + 1 > dp[i]) {                dp[i] = dp[j] + 1;                p[i] = j;                if (dp[i] > Max) {                    Max = dp[i];                    Maxi = i;                }            }        }    }    printf ("%d\n", Max);    int cnt = 0;    while (Maxi) {        ans[cnt++] = Maxi;        Maxi = p[Maxi];    }    while (Max--) {        printf ("%d\n", a[ans[Max]].key);    }    return 0;}