HDU 1160:FatMouse's Speed(LIS+记录路径)

                               FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20803    Accepted Submission(s): 9227
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

题意

有若干老鼠,每个老鼠都有体重和奔跑的速度。找到一个最长的老鼠序列,使老鼠的体重递增,速度递减,并输出这些老鼠的编号

思路

将老鼠的体重按照递增顺序排序,然后查找符合题目要求的老鼠序列,同时记录下老鼠排序之后的老鼠的编号。然后找到最长序列中的最后一个老鼠(体重最小,速度最大),然后递归寻找就行了

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,-1,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int dp[maxn];
// vis[i]=j  表示序列中第i个老鼠的前一个老鼠是第j个老鼠
int vis[maxn];
struct wzy
{
	int weight,speed;
	int id;
}p[maxn];
bool cmp(wzy u,wzy v)
{
	return u.weight>v.weight;
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int k=0;
	while(cin>>p[k].weight>>p[k].speed)
	{
		p[k].id=k+1;
		dp[k]=1;
		k++;
	}
	sort(p,p+k,cmp);
	int ans=0;
	int res;
	ms(vis);
	for(int i=0;i<k;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(p[i].weight<p[j].weight&&p[i].speed>p[j].speed)
			{
				if(dp[i]<dp[j]+1)
				{
					dp[i]=dp[j]+1;
					vis[i]=j;
				}
			}
		}
		if(dp[i]>ans)
		{
			ans=dp[i];
			res=i;
		}
	}
	cout<<ans<<endl;
	// res=-1时表示该序列已经输出完全
	while(res!=-1)
	{
		cout<<p[res].id<<endl;
		// 更新老鼠编号
		res=vis[res];
	}
	return 0;
}
posted @ 2018-08-24 11:19  友人-A  阅读(152)  评论(0编辑  收藏  举报