码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1160 FatMouse's Speed 解题报告

时间:2014-09-30 17:53:39      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   for   sp   div   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160

题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z)。每只老鼠有对应的weight 和 speed。现在需要从这 n 只老鼠的序列中,找出最长的一条序列,满足老鼠的weight严格递增,speed严格递减。

      我们可以用一个结构体来保存老鼠的信息,包括weight, speed 以及 id(这个 id 是未排序之前的,为了输出最后信息)。那么首先对 weight 进行递增排序,如果遇到相同的weight,就对speed进行递减排序。那么固定了weight,我们就可以着眼于对speed的处理,此时问题就变成求最长递减序列啦。由于要保存路径信息,代码用path[]来保存,递归输出即可。

      这条题目拖了好久啊,差不多一年了= =,太懒~~~

     

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 1000 + 10;
 9 
10 struct node
11 {
12     int id;
13     int weight, speed;
14 }mouse[maxn];
15 int path[maxn];
16 int cnt[maxn];
17 
18 int cmp(node a, node b)
19 {
20     if (a.weight == b.weight)
21         return a.speed > b.speed;
22     return a.weight < b.weight;
23 }
24 
25 void output(int pos)
26 {
27     if (!path[pos])
28         return;
29     output(path[pos]);
30     printf("%d\n", path[pos]);
31 }
32 
33 int main()
34 {
35     int w, sp, len = 1;
36     #ifndef ONLINE_JUDGE
37         freopen("in.txt", "r", stdin);
38     #endif
39 
40     while (scanf("%d%d", &w, &sp) != EOF)
41     {
42         mouse[len].weight = w;
43         mouse[len].speed = sp;
44         mouse[len].id = len;
45         cnt[len++] = -1;
46     }
47     sort(mouse+1, mouse+len, cmp);
48     for (int i = 1; i < len; i++)
49     {
50         int k = 0;
51         for (int j = 1; j < i; j++)
52         {
53             if (mouse[j].speed > mouse[i].speed && mouse[j].weight < mouse[i].weight && k < cnt[j])
54             {
55                 k = cnt[j];
56                 path[mouse[i].id] = mouse[j].id;
57             }
58         }
59         cnt[i] = k;
60         cnt[i]++;
61     }
62     int ansid, ans = -1;
63     for (int i = 1; i < len; i++)
64     {
65         if (cnt[i] > ans)
66         {
67             ans = cnt[i];
68             ansid = mouse[i].id;
69         }
70     }
71     printf("%d\n", ans);
72     output(ansid);
73     printf("%d\n", ansid);
74     return 0;
75 }

 

hdu 1160 FatMouse's Speed 解题报告

标签:style   blog   http   color   io   os   for   sp   div   

原文地址:http://www.cnblogs.com/windysai/p/4002128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!