HDU-1160 FatMouse's Speed

来源:互联网 发布:知乎推荐的淘宝包店 编辑:程序博客网 时间:2024/05/16 04:23

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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
 

Sample Output
44597
 


思路:按S降序排序,在求w的最长上升子序列,顺便记录序列元素的位置即可,直接贴代码了,注释够详细了......

AC代码:

#include <iostream>#include <algorithm>using namespace std;typedef long long LL;#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)struct Mouse{int num;//输入时的序号 int w;int s;}a[1005];bool cmp(Mouse a,Mouse b){if(a.s==b.s)return a.w<=b.w;return a.s>b.s;}int main(){int ans[1005],T=0,count=0;while(cin>>a[T].w>>a[T].s){a[T].num=(++T);}sort(a,a+T,cmp);//按speed从大到小排序 int tmp=0;int ans_c=0,p[1001];for(int i=0;i<T;++i){ans[i]=1;p[i]=0;//初始化(用来记录上一元素位置)的数组 }//求排好序后的最长 w 上升子序列 for(int i=1;i<T;++i){for(int j=i-1;j>=0;--j){if(a[i].w>a[j].w&&ans[j]+1>ans[i]){ans[i]=ans[j]+1;p[i]=j;//记录当前的 i位置 所在的最长上升子序列的上一个元素的位置 }}}for(int i=0;i<T;++i){if(count<ans[i]){count=ans[i];ans_c=i;//求最长上升子序列的最大元素的位置 }}cout<<count<<endl;int final[1001];//存最长上升子序列对应的输入序号 tmp=ans_c;//根据记录好的位置来获取 输入时的序号 for(int i=0;i<count;++i){final[i]=a[tmp].num;tmp=p[tmp];}for(int i=count-1;i>=0;--i)cout<<final[i]+1<<endl;return 0;}




0 0