hdu 1160 FatMouse's Speed dp

FatMouse's Speed

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

Source
Zhejiang University Training Contest 2001

题意:
给你一串是W 和 S,让你求W严格递增,S严格递减的序列的最大值,并输出顺序,答案不唯一的

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
using namespace std;
struct node
{
	int w, s, id;
}a[100005];

struct node1
{
	int num;
	int pre;//用来储存前一个数的id
}dp[100005];
bool cmp (node a, node b)
{
	if(a.w == b.w)
		return a.s > b.s;
	return a.w < b.w;
}
int main()
{
	int i, j;
	int t = 1;
	while(~scanf("%d%d", &a[t].w, &a[t].s))
	{
		dp[t].num = 1;
		dp[t].pre = 0;
		a[t].id = t;
		t++;
	}
	sort(a + 1, a + t, cmp);
	for(i = 2; i < t; i ++)
	{
		
		for(j = 1; j < i; j ++)
		{
			if(a[i].w > a[j].w &&a[i].s < a[j].s)
			{
				if(dp[j].num + 1>dp[i].num)
				{
					dp[i].num =  dp[j].num + 1;
					dp[i].pre = j;
				}
			}
			/*	if(a[i].w > a[j].w &&a[i].s < a[j].s)
				{
					dp[i].num = max(dp[i].num, dp[j].num + 1);
					dp[i].pre = j;
				}*/ //刚开始这样写wr了一万年,因为如果dp[i].num没变,dp[i].pre就不用赋值了,诶!!!下次还是多写if少写maxO(∩_∩)O

		}
	}
	int mm=0;
	int temp=0;
	for(i = 1; i < t; i ++)
	{
		if(dp[i].num > mm)
		{
			temp=i;
			mm = dp[i].num;
		}
	}
	printf("%d\n", mm);
	stack <int> st;
	st.push(temp);
	while(dp[temp].pre != 0)
	{
		st.push(dp[temp].pre);
		temp = dp[temp].pre;
	}
	while(!st.empty())
	{
		printf("%d\n", a[st.top()].id);
		st.pop();
	}
	return 0;
}


你可能感兴趣的:(hdu 1160 FatMouse's Speed dp)