您的位置:首页 > 其它

UVA - 10131 Is Bigger Smarter?

2014-10-23 13:46 330 查看
dp,不多说。。。短板还是在于编程能力。。。

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

#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAX 1100
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: