hdu 1160 FatMouse's Speed (最长单调递减子序列+纪录路径)

链接: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; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值