码迷,mamicode.com
首页 > 其他好文 > 详细

hdu(1160)——FatMouse's Speed

时间:2015-08-12 14:46:18      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:动态规划   lis   

题意:

现在给你一些数据输入,第一个代表的是体积,第二个代表的是它的速度。然后题目让你找到当体积递增但是速度递减时的最长的一个子序列,注意这里体积是要严格的递增的,速度则是要严格的递减的。最后要你把他们的序号输出。

思路:

是不是有点感觉像LIS问题。但是这里还是有点差别的。因为它要让你记录。

首先我们先对体积从小到大排序,然后我们对速度进行最长递减子序列的查询。

这里的记录前驱和我们上次的那个迷宫问题(poj 3984)一样,只需要另外开一个数组就好了。

链接:http://blog.csdn.net/acmer_hades/article/details/47069073

感觉掌握了一个题,就可以引申到另外一个题目中去的。毕竟方法都是差不多的。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
#define maxn 1111
int dp[maxn],fa[maxn];
struct node{
	int w,s;
	int idx;
}a[maxn];
bool cmp(node a,node b){
	return a.w<b.w;
}
void print(int t){
	if(fa[t]==-1){
		printf("%d\n",a[t].idx);
		return;
	}
	print(fa[t]);
	printf("%d\n",a[t].idx);
}
int main(){
	int n=1;
	while(~scanf("%d%d",&a[n].w,&a[n].s)){
		a[n].idx=n++;
	}
	sort(a+1,a+n,cmp);		//注意这里要从小到大排序,要不然会WA。 
	fill(dp,dp+n,1);
	memset(fa,-1,sizeof(fa));
	int dp_max=0,nmax=0,f=0;
	//以下是重点,与平时所写的LIS有点区别; 
	for(int i=1;i<=n-1;i++){
		f=-1;
		dp_max=0;
		for(int j=1;j<i;j++){
			if(a[i].s<a[j].s&&a[i].w>a[j].w){
				if(dp[j]>dp_max){
					dp_max=dp[j];
					f=j;
				}
			}
		}
		dp[i]=dp_max+1;
		fa[i]=f;
		if(dp[i]>dp[nmax]) nmax=i;
	}
	int len=dp[nmax];
	printf("%d\n",len);
	print(nmax);
}
/*
9
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900 
*/

Get better day by day!


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu(1160)——FatMouse's Speed

标签:动态规划   lis   

原文地址:http://blog.csdn.net/acmer_hades/article/details/47442829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!