题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)

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
解题思路:将速度降序排,从后往前求最长递减子序列(与LIS的条件相反),同时记录一下LIS的所有下标,每一轮更新一下左端点值max2,最后按着max2=t[max2]沿路打印LIS的所有下标,还有此题是特判,因此只需给出满足条件中的一种即可。
AC代码(15ms):
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int w,s,index;//w,s,index分别是体重、速度和LIS的下标
 5 }nod[1005];
 6 bool cmp(node a,node b){return a.s>b.s;}
 7 int dp[1005],t[1005];
 8 int main(){
 9     int k=1,max1,max2;
10     while(cin>>nod[k].w>>nod[k].s){nod[k].index=k;dp[k++]=1;}
11     sort(nod+1,nod+k,cmp);max1=max2=0;memset(t,0,sizeof(t));
12     for(int i=k-1;i>0;--i){
13         for(int j=k-1;j>i;--j)
14             if(nod[j].w>nod[i].w&&nod[j].s<nod[i].s&&dp[i]<dp[j]+1){dp[i]=dp[j]+1;t[i]=j;}//记录最长子序列的下标
15         if(dp[i]>max1){max1=dp[i];max2=i;}//记录最终的下标号
16     }
17     cout<<max1<<endl;
18     while(max2!=0){
19         cout<<nod[max2].index<<endl;
20         max2=t[max2];
21     }
22     return 0;
23 }
原文地址:https://www.cnblogs.com/acgoto/p/9538837.html