贪心算法-ACM Gone Fishing-POJ 1042

http://blog.csdn.net/ju136/article/details/6957771

原题http://poj.org/problem?id=1042

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<set>
 5 #include<iostream>
 6 using namespace std;
 7 
 8 class lake
 9 {
10 public:
11     int id;
12     int fishes;
13     int dec;
14 public:
15     lake(){}
16     lake(int a, int b, int c):id(a), fishes(b), dec(c){}
17     friend bool operator < (const lake &a, const lake &b)
18     {
19         if (a.fishes == b.fishes) return a.id < b.id;
20         return a.fishes > b.fishes;
21     }
22 };
23 
24 int n, h, f[30], d[30], t[30];
25 int max_fish_times[30];
26 int temp_fish_times[30];
27 
28 int main(void)
29 {
30     while (scanf("%d", &n) != EOF && n)
31     {
32         scanf("%d", &h); h *= 12;
33         for (int i = 0; i < n; ++i) scanf("%d", f + i);
34         for (int i = 0; i < n; ++i) scanf("%d", d + i);
35         for (int i = 1; i < n; ++i) scanf("%d", t + i);
36         for (int i = 1, s = 0; i < n; ++i) {s += t[i]; t[i] = s;}
37 
38         int max_fishes = -1;
39         memset(max_fish_times, 0, sizeof(max_fish_times));
40 
41         for (int stop_lake = 0; stop_lake < n; ++stop_lake)
42         {
43             const int max_fishing_times = (h - t[stop_lake]);
44 
45             set<lake> min_lakes;
46             for (int i = 0; i <= stop_lake; ++i) min_lakes.insert(lake(i, f[i], d[i]));
47 
48             int sum_fishes = 0;
49             memset(temp_fish_times, 0, sizeof(temp_fish_times));
50             for (int fish_time_th = 0; fish_time_th < max_fishing_times; ++fish_time_th)
51             {
52                 //选择最多的进行钓鱼。
53                 lake temp_lake = *(min_lakes.begin());
54                 min_lakes.erase(min_lakes.begin());
55                 sum_fishes += temp_lake.fishes;
56 
57                 temp_fish_times[temp_lake.id]++;
58 
59                 temp_lake.fishes -= temp_lake.dec;
60                 if (temp_lake.fishes <= 0) temp_lake.fishes = 0;
61                 min_lakes.insert(temp_lake);
62             }
63             if (sum_fishes > max_fishes)
64             {
65                 memcpy(max_fish_times, temp_fish_times, (n<<2));
66                 max_fishes = sum_fishes;
67             }
68         }
69 
70         for (int i = 0; i < n - 1; ++i)
71             printf("%d, ", max_fish_times[i]*5);
72         printf("%d\n", max_fish_times[n-1]*5);
73         printf("Number of fish expected: %d\n\n", max_fishes);
74     }
75     return 0;
76 }
posted @ 2012-10-22 21:24  菜鸟程序员的奋斗&  阅读(429)  评论(0编辑  收藏  举报