贪心算法-ACM Gone Fishing-POJ 1042

本文提供了一种解决POJ 1042问题的有效算法。通过定义湖泊类并使用集合来存储湖泊信息,该算法能够在限定时间内最大化捕鱼数量,并输出预期的捕鱼次数及总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/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 }

转载于:https://www.cnblogs.com/thly1990/archive/2012/10/22/2734559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值