HDU1160 FatMouse's Speed(lis)

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20657    Accepted Submission(s): 9154
Special Judge

 

Problem Description

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(这个题滴Output阔能有一丝丝问题,或者是我IDE的问题。我得到的答案是4 4 5 9 8POJ VJ都AC了)

分析:本题为一个最长上升子序列问题,可是从什么地方可以看得出来呢?

 优先讲体重进行按从小排序,体重相同的速度大的排前面,那就构成了最长次啊将子序列求他的长度即可。

最让我头疼的往往是记录路径的问题下面让我们举例深刻理解一下:

 代码:aa[i].pre = j;我们知道的到达一个地方可以有多条路径,那我们怎么才能记录下到达他最短的那条呢,我们可以记录他的前一个点的坐标,所需到达 的那个点的位置后,我们可以用不但回溯的方法。获得他所走的路径。

举个栗子吧:数:1 9 3 7 可知他的最长上升子序列为1 3 7.我们可以逐步缕一缕去他门的过程,看看否能得到他们的路径 。首先我们取1没啥子问题  ,然后取9满足条件记录其前一个坐标 1.  3的时候因为它小于9所以不取。7的时候满足条件取7,而他前一个坐标为3.所以我们记录的坐标为1 和3知道到达钱的坐标我们便可回溯到的路径。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
//最长下降子序列加输出
const int maxn = 1330;
int dp[maxn];
int k[maxn];

struct uu
{
	int x;
	int y;
	int pos;
	int pre;
}aa[maxn];
bool cmp(uu a, uu b)
{
	if (a.x == b.x)
	{
		return a.y > b.y;
	}
	else
	{
		return a.x < b.x;
	}
}
int main()
{
	int i = 1, j;
	while (scanf("%d%d", &aa[i].x, &aa[i].y) != EOF)
	{
		aa[i].pos = i;
		i++;
	}
	int n = i;
	sort(aa + 1, aa + n, cmp);
	memset(dp, 0, sizeof(dp));
	for (i = 1; i < n; i++)
	{
		dp[i] = 1;
		for (j = 1; j < i; j++)
		{
			if (aa[j].y > aa[i].y&&aa[j].x<aa[i].x&&dp[i]<dp[j] + 1)
			{
				dp[i] = dp[j] + 1;
				aa[i].pre = j;
				/*记录前一个的坐标。
				要知道到达一个地方,我们可能会有多条路径。
				所以我们可以记录他的前-个坐标的点,然后回溯
				就可以获得他所走地窖的路径
				举例: 1 9 3 7 最长上升子序列1 3 7;取元素9时满足条件
				pre[i] = 1;
				取3时不满足条件,取7时满足条件记录pre[i] = 3;
				经回溯可以找到所取点的位置。
				*/
			}
		}
	}
	int maxu = 0, p = 1;
	for (i = 1; i <= n; i++)
	{
		if (maxu < dp[i])
		{
			maxu = dp[i];
			p = i;
		}
	}
	cout << maxu << endl;
	for (i = 1; i < n; i++)
	{
		k[i] = p;
		p = aa[p].pre;
	}
	for (i = maxu; i >= 1; i--)
	{
		cout << aa[k[i]].pos << endl;
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值