hdoj 1160
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19783 Accepted Submission(s): 8748
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
看到这道题目我直接上LIS。
结果,自信一交,wa的一声。
看来看去看不出什么不对劲,就看别人的。
居然要排序,然后我就排序了遍。结果A了。值得思考,为什么要排序???
喔!!明白了,LIS规划的只有一组数据啊!!如果同时用LIS规划两组数据的话,只能够照顾好一边的数据。而将重量排序后,问题就变得只需要对速度进行类似的LIS。。(感觉自己好蠢qwq)
先附上没有排序的代码。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int maxn=1006; 5 int weight[maxn],speed[maxn],dp[maxn],out[maxn],rem[maxn]; 6 int n; 7 8 int LIS() 9 { 10 dp[1]=1; 11 int ans=1,num=1; 12 for(int i=2;i<n;i++){ 13 int m=0; 14 for(int j=1;j<i;j++){ 15 if(dp[j]>m&&weight[j]<weight[i]&&speed[j]>speed[i]){ 16 m=dp[j]; 17 out[i]=j; 18 } 19 } 20 dp[i]=m+1; 21 if(ans<dp[i]){ 22 ans=dp[i]; 23 num=i; 24 } 25 26 } 27 return num; 28 } 29 30 void output(int nex) 31 { 32 if(out[nex]==0){ 33 printf("%d\n",nex); 34 return ; 35 } 36 output(out[nex]); 37 printf("%d\n",nex); 38 } 39 40 int main() 41 { 42 n=1; 43 while( ~scanf("%d%d",&weight[n],&speed[n])){ 44 n++; 45 } 46 memset( dp, 0, sizeof dp); 47 memset( out, 0, sizeof out); 48 int num=LIS(); 49 int ans=0; 50 while(num!=0){ 51 rem[++ans]=num; 52 num=out[num]; 53 } 54 printf("%d\n",ans); 55 for(int i=ans;i>0;i--) 56 printf("%d\n",rem[i]); 57 58 return 0; 59 }
附上A了的代码
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=1006; 6 struct Point{ 7 int w,s; 8 int index; 9 }cargo[maxn]; 10 int dp[maxn],out[maxn],rem[maxn]; 11 int n; 12 13 bool cmp(Point a, Point b) 14 { 15 if(a.w==b.w) return a.s>b.s; 16 return a.w<b.w; 17 } 18 19 int LIS() 20 { 21 dp[1]=1; 22 int ans=1,num=1; 23 for(int i=2;i<n;i++){ 24 int m=0; 25 for(int j=1;j<i;j++){ 26 if(dp[j]>m&&cargo[j].w<cargo[i].w&&cargo[j].s>cargo[i].s){ 27 m=dp[j]; 28 out[i]=j; 29 } 30 } 31 dp[i]=m+1; 32 if(ans<dp[i]){ 33 ans=dp[i]; 34 num=i; 35 } 36 } 37 return num; 38 } 39 40 int main() 41 { 42 n=1; 43 while( ~scanf("%d%d",&cargo[n].w,&cargo[n].s)){ 44 cargo[n].index=n; 45 n++; 46 } 47 sort( cargo+1, cargo+n, cmp); 48 memset( dp, 0, sizeof dp); 49 memset( out, 0, sizeof out); 50 int num=LIS(); 51 int ans=0; 52 while(num!=0){ 53 rem[++ans]=num; 54 num=out[num]; 55 } 56 printf("%d\n",ans); 57 for(int i=ans;i>0;i--) 58 printf("%d\n",cargo[rem[i]].index); 59 60 return 0; 61 }