首页 > 代码库 > HDU1160_FatMouse's Speed【LIS变形】
HDU1160_FatMouse's Speed【LIS变形】
FatMouse‘s Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9624 Accepted Submission(s): 4284
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
Sample Output
4
4
5
9
7
题目大意:胖老鼠以为越胖的老鼠跑的越快。现在给你一些老鼠的重量和速度,
直到输入到文件结束,问:你能否找到一系列数据,来证明越胖的老鼠跑调越慢
最后输出满足条件的数据个数,并按顺序输出每组数据原本所在的行数。
思路:先用结构体存起来每只老鼠的体重、速度和所在行号。然后对结构体排序。
先按体重递增排序,再按速度递减排序。之后按最长递减序列的求法求出符合的
数据最大个数。用x[]数组存嘴上递减子序列的长度,用y[]记录排序后的最长递减
子序列倒数第二个的下标。这样能够通过往前递推的方法依次找到最长递减子序
列的路径。用z[]数组记录路径,并输出。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int a; int b; int i; }num[1010]; int x[1010],y[1010],z[1010]; bool cmp(node x,node y) { if(x.a==y.a) return x.b > y.b; return x.a < y.a; } int main() { int M,N; int i = 1; memset(num,0,sizeof(num)); while(~scanf("%d%d",&M,&N)) { num[i].i = i; num[i].a = M; num[i++].b = N; } sort(num+1,num+i,cmp); int Max = 0,pos = -1; for(int j = 1; j < i; j++) { x[j] = 1; y[j] = -1; } for(int j = 2; j < i; j++) { for(int k = 1; k < j; k++) { if(num[k].a < num[j].a && num[k].b > num[j].b && x[k]+1>x[j])//满足体重增,速度减 { x[j] = x[k]+1; y[j] = k;//记录排序后的最长子序列倒数第二个的下标 } } if(x[j] > Max)//最长子序列的长度和最后一位的下标 { Max = x[j]; pos = j; } } int count = 0; while(pos != -1)//得到 { z[count++] = pos; pos = y[pos]; } printf("%d\n",Max); for(int i = count-1; i>=0; i--) printf("%d\n",num[z[i]].i); return 0; }
HDU1160_FatMouse's Speed【LIS变形】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。