hdu1160最长上升子序列

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <stack>

using namespace std;
struct node
{
    int w;
    int s;
    int pre;
    int x;
} a[1009],b[1009];
int cmp(node a,node b)
{
    if(a.w==b.w) return a.s>b.s;
    else if(a.s==b.s) a.w<b.w;
    else return a.w<b.w;
}
int dp[1009];

int main()
{
    int i,j,k,n,maxx=-1;
    i=1;
    n=0;
    while(~scanf("%d%d",&a[i].w,&a[i].s))
    {
        a[i].x=i;
        a[i].pre=-1;
        i++;
        n++;
    }
    //printf("n==%d\n",n);
    sort(a+1,a+n+1,cmp);
    /*for(i=1;i<=n;i++)
    {
        //printf("i==%d : %d %d\n",i,a[i].w,a[i].s);
    }*/
    for(i=1; i<=n; i++)
    {
        dp[i]=1;
        for(j=1; j<=i-1; j++)
        {
            if(a[j].w<a[i].w&&a[j].s>a[i].s)
            {
                if(dp[i]<dp[j]+1)
                {
                    dp[i]=dp[j]+1;
                    a[i].pre=j;
                }
            }
        }
        if(maxx<dp[i])
        {
            maxx=dp[i];
            k=i;
        }
    }
    stack<int>q;
    printf("%d\n",maxx);
    while(k!=-1)
    {
        q.push(a[k].x);
        k=a[k].pre;
    }
    while(!q.empty())
    {
        printf("%d\n",q.top());
        q.pop();
    }

}/*
9
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
*/

排个序,做好标记就可以了。


猜你喜欢

转载自blog.csdn.net/keepcoral/article/details/80102184