Hduoj1160【DP】

/*FatMouse's Speed 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Special Judge 
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
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

Source
Zhejiang University Training Contest 2001 
*/ 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mice
{
	int w, s, num;
}m[1010];
int n, sum, max, dp[1010], Max[1010];
int cmp(const void*a, const void *b)
{
	struct mice *c = (mice *)a;
	struct mice *d = (mice *)b;
	if(c->w != d->w)
	return c->w - d->w;
	else
	return d->s - c->s;
}

int main()
{
	int i, j, k;
	i = 1;
	while(scanf("%d%d", &m[i].w, &m[i].s) != EOF)
	{
		m[i].num = i;
		++i;
	}
	n = i;
	qsort(m+1, n, sizeof(m[0]), cmp);//根据题目要求进行排序,便于遍历寻找答案 
	sum = 0;
	max = 0;
	memset(dp, 0, sizeof(dp));
	memset(Max, 0, sizeof(Max));//用来记录每一位的前缀 
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j < i; ++j)
		{
			if(m[j].w < m[i].w && m[j].s > m[i].s)
			{
				sum = dp[j] + 1;
				if(sum > dp[i])
				{
					dp[i] = sum;
					Max[i] = j;
				}
			}
		}
		if(!dp[i])
		dp[i] = 1;
		if(dp[i] > max)
		{
			max = dp[i];//记录最大值 
			k = i;//记录最后一位 
		}
	}
	printf("%d\n", max);
	int put[1010];//记录输出 
	j = 0;
	for(i = k; i > 0; )//逆序存储 
	{
		put[j++] = i;
		i = Max[i];
	}
	for(i = j-1; i >= 0; --i)//逆序输出 
	printf("%d\n", m[put[i]].num);
	return 0;
}


题意:给出n对数字,每一对的前一个代表体重,后一个代表速度,现要求从这n对中找出序列尽可能长的满足体重严格递增的同时,速度是严格递减的。输出对数和这n对的编号。

思路:首先肯定要对这n对数据根据题目要求进行排序的,接下来如果用暴力的话会超时,所以必须得想到DP的方法,思路 就是在排好序的情况下,第i对数据对于前面的数据来说要么满足要么不满足,满足条件,显然对数dp【i】=dp【j】+1;求出对于第i对来说的最大对数,其次边求dp边统计最大的对数,由于要输出最大对数的路径,所以要弄一个数组记录每一对数据的前一位,最后逆序输出结果就是了。

你可能感兴趣的:(Hduoj1160【DP】)