UVA - 10131 Is Bigger Smarter?

来源:互联网 发布:java 下载https图片 编辑:程序博客网 时间:2024/06/04 01:29

dp,不多说。。。短板还是在于编程能力。。。

遇到一个目前还没搞清楚的问题,elephant里重载小于号存在问题,这里本地codeblocks必须加const,oj上却不用加。。。。重新回去学c++吧。。

#include<cstdio>#include<algorithm>#include<cstring>#define MAX 1100using namespace std;struct elephant{    int w,i,index;    bool operator < (elephant other)const    {        if(w<other.w)            return true;        else if(w==other.w&&i>=other.i)            return true;        return false;    }}ele[MAX];int dp[MAX],father[MAX];void print(int index){    if(index<0)        return ;    print(father[index]);    printf("%d\n",ele[index].index);}int main(){    int a,b,i=0,j,t,maxx;    while(scanf("%d %d",&a,&b)!=EOF)    {        ele[i].index=i+1,ele[i].w=a,ele[i].i=b;        //printf("%d %d %d\n",ele[i].w,ele[i].i,ele[i].index);        i++;    }    //printf("%da",i);    sort(ele,ele+i);    //for(j=0;j<i;j++)    //    printf("%d %d %d\n",ele[j].w,ele[j].i,ele[j].index);    memset(dp,0,MAX*4);    dp[0]=1,father[0]=-1;    for(j=1;j<i;j++)    {        dp[j]=1,father[j]=-1;        for(t=0;t<j;t++)        {            if(ele[t].w<ele[j].w&&ele[t].i>ele[j].i)            {                if(dp[t]+1>dp[j])                {                    dp[j]=dp[t]+1;                    father[j]=t;                }            }        }    }   maxx=0;    for(j=1;j<i;j++)        if(dp[j]>dp[maxx])        maxx=j;    printf("%d\n",dp[maxx]);    print(maxx);    return 0;}


0 0
原创粉丝点击