FatMouse's Speed(dp)

今天心情好差啊!刷刷题...

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream> 
#include <algorithm>  
using namespace std; 
struct node
{
    int w,s,no;
}a[1005];
bool cmp(node a,node b)
{
    if(a.w==b.w)
        return a.s>b.s;
    return a.w<b.w;
}
int main()
{
    int i=0,j,n,max,t;
    int dp[1005],from[1005];
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d",&a[i].w,&a[i].s)!=EOF)
    {
        a[i].no=i+1;
        i++;
    }
    n=i;
    sort(a,a+n,cmp);
    dp[0]=1;
    for(i=1;i<n;i++)
    {
        dp[i]=1;
        for(j=0;j<i;j++)
        {
            if(a[j].w<a[i].w&&a[j].s>a[i].s)
                if(dp[j]+1>=dp[i])
                {
                    dp[i]=dp[j]+1;
                    from[i]=j;
                }
        }
    }
    max=0;
    j=-1;
    for(i=0;i<n;i++)
    {
        if(max<dp[i])
        {
            max=dp[i];
            j=i;
        }
    }
    t=j;
    j=1;
    printf("%d\n",max);
    for(i=0;i<max;i++)
    {
        dp[max-j]=t;
        j++;
        t=from[t];
    }
    for(i=0;i<max;i++)
        printf("%d\n",a[dp[i]].no);
    return 0;
} 


你可能感兴趣的:(Algorithm,struct,ini,iostream)