HDU 1160 FatMouse's Speed

半个下午,总算A过去了

毕竟水题

好歹是自己独立思考,debug,然后2A过的

我为人人的dp算法

题意:

为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点

本着“指针是程序员杀手”这一原则,我果断用pre来表示这只老鼠的直接前驱的序号

代码中我是按体重从大到小排序,然后找出一条最长的体重严格递减速度严格递增的“链”(其实找到的是链尾)。

然后输出的时候从后往前输出

对结构体排序

对于样例来说,循环完以后应该是这样的:

order 2 3 4 8 1 5 7 0 6
weight  500 1000 1100 2000 6000 6000 6000 6008 8000
speed  2000 4000 3000 1900 2100 2000 1200 1300 1400
lenth 1 1 2 3 3 3 4 4 4
pre -1 -1 3 4 4 4 8 8 8

pre == -1代表没有前驱。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 struct Mouse
 9 {
10     int order;
11     int weight;
12     int speed;
13     int pre;
14     int lenth;
15 }mice[1010];
16
17 int a[1010];
18
19 bool cmp1(Mouse a, Mouse b)
20 {
21     if(a.weight != b.weight)
22         return (a.weight > b.weight);
23     return (a.speed < b.speed);
24 }
25
26 bool cmp2(Mouse a, Mouse b)
27 {
28     return (a.order < b.order);
29 }
30
31 int main(void)
32 {
33     #ifdef LOCAL
34         freopen("1160in.txt", "r", stdin);
35     #endif
36
37     int cnt = 0;
38     int w, s;
39     while(scanf("%d%d", &w, &s) == 2)
40     {
41         mice[cnt].weight = w;
42         mice[cnt].speed = s;
43         mice[cnt].order = cnt;
44         ++cnt;
45     }
46
47     sort(mice, mice+cnt, cmp1);
48     int i, j;
49     for(i = 0; i < cnt; ++i)
50     {
51         mice[i].lenth = 1;
52         mice[i].pre = -1;
53     }
54     //我为人人
55     for(i = 1; i < cnt; ++i)
56     {
57         for(j = 0; j < i; ++j)
58         {
59             if(mice[j].weight > mice[i].weight
60                 && mice[j].speed < mice[i].speed
61                 && mice[j].lenth+1 > mice[i].lenth)
62             {
63                 mice[i].lenth = mice[j].lenth + 1;
64                 mice[i].pre = mice[j].order;
65             }
66         }
67     }
68
69     sort(mice, mice+cnt, cmp2);
70     int maxlen = 0;
71     for(i = 1; i < cnt; ++i)
72         if(mice[i].lenth > mice[maxlen].lenth)
73             maxlen = i;
74     int l = mice[maxlen].lenth;
75     printf("%d\n%d\n", l, maxlen+1);
76
77     int p = maxlen;
78     for(i = 1; i < l; ++i)
79     {
80         printf("%d\n", mice[p].pre+1);
81         p = mice[p].pre;
82     }
83     return 0;
84 }

代码君

HDU 1160 FatMouse's Speed

时间: 2024-10-10 00:00:52

HDU 1160 FatMouse's Speed的相关文章

HDU 1160 FatMouse&#39;s Speed DP题解

本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,不过因为需要按原来的标号输出,故此需要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法,基本的一维动态规划法了. 记录路径:只需要记录后继标号,就可以逐个输出了. #include <stdio.h> #include <algorithm> using namespace std; const int MAX_N = 1005; struct MouseSpeed { int id, w, s; b

hdu 1160 FatMouse&#39;s Speed(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1160 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; struct Node { int w,s,id,fa; }; Node mice[1000+10]; int dp[100

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): 9174    Accepted Submission(s): 4061 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

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

HDU 1160 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了. 题目思路:这是个最长上升子序列的问题,我们按W的升序进行排序,若W相等则按V的降序排序.用Pre[]记录当前点的前驱节点,Last记录序列最后一个点,maxn记录最长长度,完成动规后可根据Last和Pre[]输出路径. #include<cstdio> #include<stdio.h&

[dp专题]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): 10172    Accepted Submission(s): 4521Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster i

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: 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 s

LIS [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): 9448    Accepted Submission(s): 4205 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

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