hdu1160——二维LIS+输出路径

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

Input

Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

Output

Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

W[m[1]] < W[m[2]] < ... < W[m[n]]

and

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output

4
4
5
9
7

FatMouse认为,老鼠越胖,它跑得越快。为了反驳这一点,您希望获取鼠标集合上的数据,并将此数据的子集尽可能大到序列中,以便权重增加,但速度在下降。‎

‎输入‎

‎输入包含一组鼠标的数据,每行一个鼠标,按文件结尾终止。‎
‎特定鼠标的数据将包括一对整数:第一个表示其大小(以克为单位),第二个表示以厘米/秒为单位‎
‎的速度。两个整数都在 1 和 10000 之间。每个测试用例中的数据将包含最多 1000 个鼠标的信息。‎
‎两只老鼠可能具有相同的重量,相同的速度,甚至相同的重量和‎
‎速度。‎

‎输出‎

‎程序应输出一系列数据;第一行应包含数字 n;剩余的 n 行应每行应包含一个正整数(每行表示鼠标)。如果这些 n 个整数是 m_1},m_2_,..., m_n_ ,则必须‎

‎是 W_m{1} < W_m_2_ << W_m_n_ ‎
‎ ‎


‎和 S_m{1} > S_m_2_ > ...> S_m_n_‎

‎为了正确答案,n 应尽可能大。‎
‎所有不等式都是严格的:重量必须严格增加,速度必须严格降低。给定输入可能有许多正确的输出,您的程序只需要找到一个。‎

 

这个题是个很好的LIS的变形理解题,题意就是求出满足大小严格递增和速度严格递减的最长子序列并且输出。

很容易想到,我们可以先按照一个方向sort一下,然后再用LIS求出满足条件的最长子序列,同时开个pre数组记录,最后的时候回溯输出一下就行了。

#include <iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Point{
	int w,s;
	int id;
}p[1005];
int cnt,dp[1005],pre[1005],ans[1005];
bool cmp(Point a,Point b){
	return a.s==b.s?a.w<b.w:a.s>b.s; 
}
int main(int argc, char** argv) {
	cnt=0;
	int x,y;
	while(~scanf("%d%d",&x,&y)){
		p[cnt].w=x;
		p[cnt].id=cnt+1;
		p[cnt++].s=y;
	}
	sort(p,p+cnt,cmp);
//	for(int i = 0;i<cnt;++i) printf("%d %d\n",p[i].s,p[i].w);
	memset(pre,-1,sizeof(pre));
	for(int i = 0;i<cnt;++i)
		dp[i]=1;
	for(int i = 0;i<cnt;++i)
		for(int j = 0;j<i;++j)
			if(p[j].w<p[i].w&&p[j].s>p[i].s){
				if(dp[j]+1>dp[i]){
					pre[i]=j;
//					cout<<"AC"<<endl; 
					dp[i]=dp[j]+1; 
				}
			}
	int maxn=-1,maxi=0;			
	for(int i = 0;i<cnt;++i){
		if(dp[i]>maxn){
			maxn=dp[i];
			maxi=i;
		}
	} 
	printf("%d\n",maxn);
	if(maxn==1) printf("%d\n",dp[0]);
	int k = 0;
//	for(int i = 0;i<cnt;i++) printf("%d ",pre[i]);
	for(int i = maxi;i!=-1;i=pre[i]){
		ans[k++]=i;
	}
	while(k>0){
		k--;
		printf("%d\n",p[ans[k]].id);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值