HDU - 1160 FatMouse's Speed

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17102    Accepted Submission(s): 7567
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
 
题解:
  这道题思路求最长递增序列,还是推荐先做嵌套矩形http://www.cnblogs.com/creativepower/p/7324575.html
  理解透彻嵌套矩形,这道题也容易做出来了。
  这题意思是,求体重递增且速度递减的最长序列个数,最后还要输出最长递增序列的路径
  
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 int pre[1010];
 6 struct node {
 7     int w, sp, v;
 8 }s[1010];
 9 bool cmp(node a, node b) {
10     return a.w < b.w;
11 }
12 void fun(int n) {//打印路径
13     if(pre[n] != n) fun(pre[n]);
14     printf("%d\n",n+1);
15 }
16 int main()
17 {
18     int n = 0;
19     int dp[1010];//储存序列的长度
20     
21     while(scanf("%d%d",&s[n].w,&s[n].sp) != EOF) {
22         pre[n] = n;
23         s[n].v = n;
24         dp[n] = 1;
25         n++;
26     }
27     sort(s,s+n,cmp);
28     for(int i = 1 ; i < n ; i++) {
29         for(int j = 0; j < i; j ++) {
30             if(s[i].w > s[j].w && s[i].sp < s[j].sp)
31                 if(dp[i] < dp[j]+1) {
32                     pre[s[i].v] = s[j].v;//存路径,将s[j].v指向s[i].v
33                     dp[i] = dp[j]+1;
34                 }
35         }
36     }
37     int ans = 0;
38     int mark = -1;
39     for(int i = 0; i < n; i++) {//找到路径最长的点
40         if(ans < dp[i]) {
41             ans = dp[i];
42             mark = s[i].v;
43         }
44     }
45     printf("%d\n",ans);
46     fun(mark);//打印路径
47     return 0;
48 }

 

  
posted @ 2017-08-16 11:13  启动  阅读(149)  评论(0编辑  收藏  举报