Uva--757(贪心,优先队列)

2014-07-27 14:36:04

  Gone Fishing 

John is going on a fishing trip. He has h hours available ( $1 \le h \le 16$), and there are n lakes in the area ( $2 \le n \le 25$) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each $i = 1, \dots, n- 1$, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti ( $0 < t_i \le 192$). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4.


To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi ( $f_i \ge 0$), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di ( $d_i \ge 0$). If the number of fish expected to be caught in an interval is less than or equal to di, there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.


Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

Input 

You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi ( $1 \le i \le n$), then a line of n integers di ( $1 \le i \le n$), and finally, a line of n - 1 integers ti ($1 \le i \le n - 1$). Input is terminated by a case in which n = 0.

Output 

For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected. If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases. 

Sample Input 

2
1
10 1
2 5
2
4
4
10 15 20 17
0 3 4 3
1 2 3
4
4
10 15 50 30
0 3 4 3
1 2 3
0

Sample Output 

45, 5
Number of fish expected: 31

240, 0, 0, 0
Number of fish expected: 480

115, 10, 50, 35
Number of fish expected: 724

思路:首先根据贪心策略可知,人在钓过一个Lake后不会再回来钓,枚举终点(0 - n-1)然后把走中间路程的时间减掉,这样可以认为人可以在各个Lake间瞬移,所以可以用优先队列,每次选择能钓到最多鱼的Lake(贪心)即可。本题有个坑,就是要尽量在序号小的Lake里面"浪费时间",所以在定义优先队列优先级时:在能钓到鱼数相等时序号小的优先级更高。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <queue>
 5 #include <vector>
 6 #include <functional>
 7 #include <iostream>
 8 using namespace std;
 9 
10 struct Lake{
11     int id;
12     int f,d;
13     int now;
14     friend bool operator < (Lake a,Lake b){
15         if(a.now == b.now)
16             return a.id > b.id;
17         return a.now < b.now;
18     }
19 }l[30];
20 
21 int main(){
22     int n,h,t[30] = {0},tmp,time,head = 1;
23     while(scanf("%d",&n) == 1 && n){
24         scanf("%d",&h);
25         priority_queue<Lake> q;
26         for(int i = 0; i < n; ++i){
27             scanf("%d",&l[i].f);
28             l[i].id = i;
29         }
30         for(int i = 0; i < n; ++i)
31             scanf("%d",&l[i].d);
32         for(int i = 1; i < n; ++i){
33             scanf("%d",&tmp);
34             t[i] = t[i - 1] + tmp;
35         }
36         //枚举:最远到哪个Lake
37         int ans[30],tans[30],cnt,ansmax = -1;
38         memset(ans,0,sizeof(ans));
39         for(int i = 0; i < n; ++i){
40             cnt = 0;
41             memset(tans,0,sizeof(tans));
42             time = h * 60 - t[i] * 5;
43             if(time <= 0) continue;
44             for(int j = 0; j <= i; ++j){
45                 l[j].now = l[j].f;
46                 q.push(l[j]);
47             }
48             while(time > 0){
49                 Lake p = q.top();
50                 q.pop();
51                 if(p.now <= 0){
52                     tans[0] += time;
53                     break;
54                 }
55                 time -= 5;
56                 if(time < 0)
57                     break;
58                 tans[p.id] += 5;
59                 cnt += p.now;
60                 p.now -= p.d;
61                 if(p.now < 0) p.now = 0;
62                 q.push(p);
63             }
64             if(cnt > ansmax){
65                 ansmax = cnt;
66                 memcpy(ans,tans,sizeof(tans));
67             }
68             else if(cnt == ansmax){
69                 int flag = 0;
70                 for(int j = 0; j < n; ++j){
71                     if(tans[j] == ans[j]) continue;
72                     if(tans[j] > ans[j]) flag = 1;
73                     break;
74                 }
75                 if(flag)
76                     memcpy(ans,tans,sizeof(tans));
77             }
78         }
79         if(!head)
80             printf("\n");
81         head = 0;
82         printf("%d",ans[0]);
83         for(int i = 1; i < n; ++i) printf(", %d",ans[i]);
84         printf("\nNumber of fish expected: %d\n",ansmax);
85     }
86     return 0;
87 }

 

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3871382.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值