HDU 1160 FatMouse's Speed
kuangbin专题十二:J题
输出最长上升子序列的长度 并且输出 最长上升子序列的成员排序
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17452 Accepted Submission(s): 7719
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.
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.
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
题意:求 重量递增 速度递减的 最长上升子序列 并输出这个子序列的成元排序(可以和输入状态相比是无序状态)
思路:由于这个最长上升子序列的成员排序可以是与原输入状态相比的无序状态 。 所以要想办法给数据排序 然后按照正常的最长上升子序列方法进行
(1)对重量上升排序 , dp 速度 (下降排序也可以 , 不过要改变 dp 公式判别 , (2)一样)
(2)对速度递减排序 , dp 重量
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std ; #define maxn 2000 struct node { int w , s ; int index ; //最初的序号,避免排序后乱掉顺序 }; node num[maxn] ; int pre[maxn] ; //记录i对应的上一个数据 int dp[maxn] ; //dp[i]表示以第i个数据结尾的符合要求的子列长度 int res[MAXN+10];//存放最终结果下标 // 用res数组输出结果 或者函数递归输出 int maxi ; int maxlen ; bool cmp(node a , node b){ //重量递增排序 , 重量相等 速度递减排序 if(a.w < b.w) return true ; else if(a.w == b.w ) return a.s > b.s ; else return false ; } void print(int i){ if(i == 0 ){ return; } print(pre[i]) ; printf("%d " , num[i].index ) ; } int main(){ int i=1 ; while(~scanf("%d %d" , &num[i].w , &num[i].s)){ dp[i] = 1 ; pre[i] = 0 ; num[i].index = i ; ++ i ; } maxlen = 0 ; //最长序列长度 int n = i - 1 ; sort(num + 1 , num + n + 1 , cmp) ; dp[1] = 1 ; for(int i=1 ; i<= n ; i++){ for(int j=1 ; j<i ; j++){ if(num[i].w>num[j].w && num[i].s < num[j].s&&dp[j]+1>dp[i]){ dp[i] = dp[j] + 1 ; pre[i] = j ; if(dp[i] > maxlen){ maxi = i ; //最长序列的最后一个数下标 maxlen = dp[i] ; } } } } printf("%d " , maxlen) ; print(maxi) ; /*数组输出操作 int t=maxi; i=0; while(t!=0) { res[i++]=t; t=pre[t]; } printf("%d ",i); while(i>0) { i--; printf("%d ",mouse[res[i]].index); } */ return 0 ; }