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.
InputInput 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.
OutputYour 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 题意是给你一串数据老鼠的体重和速度,要你找到一组最长的数组证明体重越大速度越小这个非常简单就是和求一个最长的上升子序列差不多这题还有一个要求是打印路径。所以就要继续前一个数据的下标。这题细节方面好恶心,打印路径的时候,只能按照体重最小打印到体重最大的顺序不能从体重最大到体重最小。(因为这个WA了无数遍)还有一个恶心的点是数据读入的时候,不知道如何终止数据的读入。这里需要用的文件的操作,第一次遇到这种题目,表示真心恶心。一个水题,弄成这样坑爹的题目也不容易
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6 #include <vector> 7 #include <set> 8 using namespace std; 9 10 const int mod = 1e9 + 7; 11 const int maxn = 1e4 + 10; 12 struct node { 13 int w, f, id; 14 } a[maxn] ; 15 struct ndoe1 { 16 int num, pre; 17 } dp[maxn]; 18 int cmp(node a, node b) { 19 return a.w < b.w; 20 } 21 int path[maxn]; 22 int main() { 23 int k = 0; 24 while(scanf("%d%d", &a[k].w, &a[k].f) != EOF ) { 25 a[k].id = k + 1; 26 dp[k].num = 1; 27 dp[k].pre = 0; 28 k++; 29 } 30 sort(a, a + k, cmp ); 31 int ans = -1, temp = 1; 32 for (int i = 0 ; i < k ; i++ ) { 33 for (int j = 0 ; j < i ; j++) { 34 if (a[i].w > a[j].w && a[i].f < a[j].f ) { 35 if (dp[i].num < dp[j].num + 1) { 36 dp[i].num = dp[j].num + 1; 37 dp[i].pre = j; 38 } 39 } 40 } 41 if (dp[i].num > ans) { 42 ans = dp[i].num; 43 temp = i; 44 } 45 } 46 printf("%d\n", ans); 47 for (int i = 0 ; i < ans ; i++ ) { 48 path[i] = temp; 49 temp = dp[temp].pre; 50 } 51 for (int i = ans - 1 ; i >= 0 ; i-- ) { 52 printf("%d\n", a[path[i]].id); 53 } 54 return 0; 55 }
FatMouse's Speed ~(基础DP)打印路径的上升子序列
原文地址:https://www.cnblogs.com/qldabiaoge/p/8776264.html