Fork me on GitHub

HDU ACM 1051/ POJ 1065 Wooden Sticks

Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8899    Accepted Submission(s): 3630

【解题思路】初学贪心仅知道一些理论的知识,这题说是贪心自己也没啥感觉,做题的思路是在抛开贪心的概念然后按照自己的想法实现出来,实现之后想在思路中找到贪心的影子,只能看到每次都是找尽可能多的stick仅此而已,Pursuiting... 

Wa的原因是当时没考虑到stick的选择可以是不连续的,直接将排序后的次元素的比较上将后面一个跟前面一个进行比较。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define SIZE 5002
 6 
 7 using namespace std;
 8 
 9 typedef struct sticks{
10     int L, w;
11 }sticks;
12 sticks stick[SIZE];
13 bool visit[SIZE]; 
14 int n;
15 
16 bool cmp(const sticks& a, const sticks& b)
17 {
18     if(a.w == b.w)
19         return a.L < b.L;
20     else return a.w < b.w;
21 }
22 
23 int main()
24 {
25     #ifndef ONLINE_JUDGE
26     freopen("input.txt", "r", stdin);
27     #endif
28     int T, sum;
29     scanf("%d", &T);
30     while(T--)
31     {
32         scanf("%d", &n);
33         for(int i=0; i<n; ++i)
34             scanf("%d%d", &stick[i].L, &stick[i].w);
35         sort(stick, stick+n, cmp);
36         sum = 0;
37         memset(visit, false, sizeof(visit));
38         int curmax = 0;
39         for(int i=0; i<n; ++i)
40         {
41             if(visit[i]) continue;
42             curmax = stick[i].L;
43             sum++;
44             for(int j=i+1; j<n; ++j)
45             {
46                 if(!visit[j] && stick[j].L >= curmax)
47                 {
48                     visit[j] = true;
49                     curmax = stick[j].L;
50                 }
51             }    
52         }
53 /*        for(int i=0; i<n; ++i)
54             if(!i || stick[i].L<stick[i-1].L) sum++;
55 */
56         printf("%d\n", sum);
57     }
58     return 0;
59 }

 

 

posted @ 2013-07-19 08:58  Gifur  阅读(340)  评论(0编辑  收藏  举报
TOP