1. 程式人生 > >HDU 1160 FatMouse's Speed(dp)

HDU 1160 FatMouse's Speed(dp)

Total Submission(s): 20872 Accepted Submission(s): 9269
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

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#define clear1(a,b) memset(a,b,sizeof(a));
using namespace std;
struct node
{
	int w;
	int p;
	int num;
}ac[10005];
bool cmp(node a,node b){
	if(a.w==b.w)
	return a.p>b.p;
	return a.w<b.w;
}
int dp[10005];
int pre[10005];
int main()
{
	int cnt=1,star;
	while(scanf("%d%d",&ac[cnt].w,&ac[cnt].p)!=EOF){
		ac[cnt].num=cnt;
		cnt++;
		//cout<<ac[cnt-1].num<<endl;
		//if(cnt==10)break;
	}
	sort(ac+1,ac+cnt,cmp);
//	int n;scanf("%d",&n);
	/*for(int i=1;i<=cnt;i++){
		cout<<ac[i].w<<" "<<ac[i].p<<" "<<ac[i].num<<endl;
	}*/
	clear1(pre,-1);
	int ans=0;
	for(int i=1;i<=cnt;i++){
			dp[i]=1;
		for(int j=0;j<i;j++){
			if(ac[j].w<ac[i].w&&ac[j].p>ac[i].p&&dp[i]<dp[j]+1){
				dp[i]=dp[j]+1;
				pre[i]=j;
			}
		}
		if(dp[i]>ans){
			ans=dp[i];
			star=i;
		}
	}
	vector<int>v;
    v.clear();
	printf("%d\n",ans);
	while(star!=-1){
		v.push_back(ac[star].num);
		star=pre[star];
	}
	for(int i=v.size()-1;i>=0;i--){
		printf("%d\n",v[i]);
	}
	return 0;
}