poj 1065 Wooden Sticks

题目大意:有一堆木棍,属性有长度和重量。它们需要被处理,若后面的长度和重量都大于前面的,消耗为0,否则为1。第一个消耗为1,求按一定的顺序处理,最小消耗是多少?

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1 <= n <= 5000 , that represents the number of wooden sticks in the test case, and the second line contains 2n positive integers l1 , w1 , l2 , w2 ,..., ln , wn , each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

3 
5 
4 9 5 2 2 1 3 5 1 4 
3 
2 2 1 1 2 2 
3 
1 3 2 2 3 1 

Sample Output

2
1
3

这个题我们先按长度排序,接着去寻找重量的不下降子序列的个数,这个个数就是消耗值
代码如下
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 typedef pair<int ,int> Stick;//length, weight
10 Stick stick[10002];
11 int flag[10002];
12 
13 int cmp(Stick a, Stick b) {
14     if(a.first == b.first) {
15         return a.second < b.second;
16     }
17     return a.first < b.first;
18 }
19 
20 int t,n;
21 
22 int main(int argc, char const *argv[])
23 {
24     //freopen("input.txt","r",stdin);
25     scanf("%d",&t);
26     while(t--) {
27         scanf("%d",&n);
28         for(int i = 0; i < n; i++) {
29             scanf("%d %d",&stick[i].first,&stick[i].second);
30         }
31         sort(stick, stick+n, cmp);
32         
33         memset(flag, 0, sizeof(flag));
34         
35         bool ok = false;
36         int cost = 0;
37         
38         while(!ok) {
39             int last = -1;
40             ok = true;
41             for(int i = 0; i < n; i++) {
42                 if(!flag[i] && (last == -1 || stick[i].second >= stick[last].second)) {
43                     flag[i] = 1;
44                     last = i;
45                     ok = false;
46                 }
47             }
48             if(ok == false) {
49                 cost++;
50             }
51         }
52         printf("%d
", cost);
53         
54     }
55 
56     return 0;
57 }
原文地址:https://www.cnblogs.com/jasonJie/p/5837064.html