`

UVA 10131 Is Bigger Smarter?

阅读更多
//  [解题方法]
//  对大象增加编号属性i,以免排序后丢失
//  对大象数组倒过来sort一下(W大的在前;若W一样,S小的在前)
//  对sort好的数组倒过来dp最长子序列,记录前驱
//  输出路径(由于是倒过来dp,所以输出路径不用栈,不断输出前驱即可)
//  复杂度O(n^2)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#define M 1005

struct point {
    int x, y, i;
}p[M];

bool cmp (point a, point b)
{
    if (a.x == b.x) return a.y < b.y;
    return a.x > b.x;
}

int nxt[M], dp[M];

int main()
{
    int n = 0, i, j, maxs, v = -1;
    while (cin >> p[n].x >> p[n].y) {
        p[n].i = n+1;
        ++n;
    }
    sort (p, p+n, cmp);
    maxs = 0;
    for (i = 0; i < n; i++) {
        dp[i] = 1;
        nxt[i] = -1;
    }
    for (i = 0; i < n; i++)
    {
        for (j = i+1; j < n; j++)
        {
            if (p[j].x < p[i].x && p[j].y > p[i].y && dp[j] < dp[i]+1)
            {
                dp[j] = dp[i] + 1;
                nxt[j] = i;
                if (dp[j] > maxs) {
                    maxs = dp[j];
                    v = j;
                }
            }
        }
    }
    cout << maxs << endl;
    while (v >= 0)
    {
        cout << p[v].i << endl;
        v = nxt[v];
    }
    return 0;
}
1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics