FatMouse's Speed ~(基础DP)打印路径的上升子序列

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

时间: 2024-10-10 15:20:26

FatMouse's Speed ~(基础DP)打印路径的上升子序列的相关文章

FatMouse&#39;s Speed 基础DP

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13643    Accepted Submission(s): 6011Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster i

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed 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

【HDU】 1160 FatMouse&#39;s Speed (DP)

一开始写的dfs进行记忆化结果不知道怎么进行路径的记录...改成循环就好了 dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i] 一开始进行一次排序使得重量递增,这样只需要考虑速度就好了 #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10005; struct Mou

HDU 1160 FatMouse&#39;s Speed (sort + dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体按照体重从小到大排序,然后根据速度就是最长下降子序列. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4

[HDOJ1160]FatMouse&#39;s Speed(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 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 sequen

HDU 1160 FatMouse&#39;s Speed (最长有序的上升子序列)

题意:给你一系列个w,s,要你找到最长的n使得 W[m[1]] < W[m[2]] < ... < W[m[n]] and S[m[1]] > S[m[2]] > ... > S[m[n]] 即在这n个w,s中满足w[i]<w[j]&&s[i]>s[j],要求:体重严格递增,速度严格递减,原始顺序不定 首先将s从大到小排序,即顺数固定后转化为最长上升子序列问题. 案例: 6008 1300 6000 2100 500 2000 1000 40

HDU 1160 FatMouse&#39;s Speed (动态规划、最长下降子序列)

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24573    Accepted Submission(s): 10896Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

uva 116 Unidirectional TSP dp + 打印路径

// uva116 Unidirectional TSP // 这题是在紫书(page 270)上看到的,个人理解就是数塔的升级版 // dp[i][j]表示从(i,j)出发到终点所达到的最大价值 // 所以很明显j是逆序的 // 状态转移方程为 // dp[i][j] = min(dp[i][j],dp[row[k]][j+1]+mp[i][j]) // rows[k]表示三行中的一行i,i-1,i+1,特判一下,排个序 // (因为多解时输出字典序最小的值) // 这题唯一比较难的地方就是打

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无