链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
解题报告:
刚开始直接纪录路径怎么都过不了,不知道什么原因,后来开个数组纪录路径,就过了,不清楚什么原因啊!
代码:
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int cnt,f[1005],temp[1005],h[1005];
struct node
{
int s,w,n;
}M[1005];
int cmp(const node &a,const node &b)
{
return a.w == b.w ? a.s > b.s : a.w < b.w;
}
int main()
{
int sp,wi,maxn;
cnt=1;
while(scanf("%d %d",&M[cnt].w,&M[cnt].s) && M[cnt].w )
{
f[cnt]=1;
M[cnt].n=cnt;
cnt++;
}
sort(M,M+cnt,cmp);
maxn=1;
int cc=0,x=0;
h[0]=-1;
for(int i=1;i<cnt;i++)
{
for(int j=1;j<i;j++)
{
if(M[j].w<M[i].w && M[j].s > M[i].s && f[j]+1 >f[i])
{
f[i] = f[j] + 1;
h[i]=j;
if(f[i]>maxn)
{
maxn=f[i];
x=i;
}
// printf("h[%d]=%d\n",i,h[i]);
}
}
}
printf("%d\n",maxn);
int i=0;
while(x!=-1)
{
temp[i++]=M[x].n;
x=h[x];
}
for(int t=i-2; t>=0;t--)
printf("%d\n",temp[t]);
return 0;
}