uva 10131 最长子序列

来源:互联网 发布:w3cschool php教程 编辑:程序博客网 时间:2024/05/20 18:44

题意:

给大象的体重和智商,求体重按从大到小,智商从高到低的最长子序列,并输出路径。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#define LL long longusing namespace std;const int maxn = 1e3 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);struct Elephant{    int w;    int iq;    int r;} e[maxn];bool cmp(Elephant a, Elephant b){    return a.w > b.w;}int dp[maxn];int n;int path[maxn];int ans;void print(int i, int dep){    if (dep == ans)        return;    printf("%d\n", e[i].r);    print(path[i], dep + 1);    //printf("%d\n", e[i].r);}int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    #endif // LOCAL    n = 1;    while (scanf("%d%d", &e[n].w, &e[n].iq) != EOF)    {        e[n].r = n;        n++;    }    sort(e + 1, e + n + 1, cmp);    dp[0] = 0;    for (int i = 1; i < n; i++)    {      //  cout << e[i].w << " " <<e[i].iq << " " << e[i].r << endl;        dp[i] = 1;        path[i] = i;        for (int j = 1; j < i; j++)        {            if (e[i].iq > e[j].iq && dp[i] < dp[j] + 1 && e[i].w < e[j].w)            {                dp[i] = dp[j] + 1;                path[i] = j;            }        }    }    ans = -1;    int pos = 0;    for (int i = 1; i < n; i++)    {        if (ans < dp[i])        {            pos = i;            ans = dp[i];        }    }    printf("%d\n", ans);    print(pos, 0);    return 0;}


0 0
原创粉丝点击