钓鱼(贪心,优先队列)

题意描述:

john现有h个小时的空闲时间,他打算去钓鱼。john钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(john每在一个湖钓完鱼后,他只能走到下一个湖继续钓), john必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。john在每个湖中每5分钟钓的鱼数(此题中以5分钟作为单位时间),随时间的增长而线性递减。而每个湖中头5分钟可以钓到的鱼数以及每个湖中相邻5分钟钓鱼数的减少量,input中均会给出。并且John从任意一个湖走到它下一个湖的时间input中也都给出。

问题:

求一种方案,使得john在有限的h小时中可以钓到尽可能多的鱼。

 

output中需包括john在所有湖边所呆的时间,以及最后总的钓鱼数。

 

算法分析:

由于每个湖都必须经过,且只经过一次,所以john花在路中的总时间是确定的。

在这个条件下,可以想成john学会了“瞬间移动”,即:他可以在任何时间,移动到任何他想去的湖,而移动的过程无需时间。

于是,john只需在每个5分钟的开始“瞬间移动”到当前5分钟中能钓到最多的鱼的湖中,且只钓5分钟的鱼。

这样可以保证john钓到尽可能多的鱼。

只要枚举john的行程是从第一个湖到第k个湖(1<=k<=n),比较出最大的钓鱼数,就是题目所求的最佳方案。

贪心算法:

每次选择一个局部最优策略进行实施,而不去考虑对今后的影响。

 

个人心得:在算法艺术和数据结构上的例题,很慌,这种内容多,结构复杂的脑阔就痛。

不过人总是要面对的,所以我还是忍者性子看题目,看题解,才慢慢拨开迷雾。

发现自己不能很有效的把题目内容转变为有效的算法,比如他这一题中可以随意停在某个湖泊,但必须前面的要走而且只能走一次,

这样就变成了跟路程所需时间无关的一个问题了,当你停在n湖泊时,则此时减去行程,就好像有了瞬间移动的能力,

你看如果你先在1中钓了5条再去2中钓4条再返回来1钓3条不就等于1中钓8条再去2钓4条吗?

真的,能有效将题目所求转变为能够解决的算法,需要一定的提炼和压缩,还有巧妙的设计。

后面我自己用优先队列写了,超时了,后面看了题解,题解中就是把每个湖泊的时间也放在结构体中了,这样就更方便,而且判断条件也

改成了time,就有效多了!

优先队列结构体的设置

 1 struct pool
 2 {
 3    int number;
 4    int gain;
 5    int time;
 6    bool operator <(pool x)const
 7    {
 8        if(gain==x.gain)
 9          return number>x.number;
10       return gain<x.gain;
11    }
12 };

Description

John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 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,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=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( fi >= 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 (di >= 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 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=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 

Source

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 struct pool
 9 {
10    int number;
11    int gain;
12    int time;
13    bool operator <(pool x)const
14    {
15        if(gain==x.gain)
16          return number>x.number;
17       return gain<x.gain;
18    }
19 
20 }p[30];
21 int ji[30];
22 int cost[30];
23 int mtime[30];
24 int main(){
25     int n;
26     int ok=1;
27     while(scanf("%d",&n)&&n){
28             if(ok>1) printf("\n");
29     ok++;
30         int hour;
31         scanf("%d",&hour);
32         hour=hour*12;
33         for(int i=1;i<=n;i++)
34             {
35                 scanf("%d",&p[i].gain);
36                 p[i].number=i;
37                 p[i].time=0;
38             }
39       for(int i=1;i<=n;i++)
40         scanf("%d",&ji[i]);
41          cost[0]=0;
42          for(int i=2;i<=n;i++)
43           {scanf("%d",&cost[i]);
44             cost[i]+=cost[i-1];
45           }
46       int maxn=-1998;
47       int flag=1;
48        for(int i=1;i<=n;i++){
49             int sum=0;
50             priority_queue<pool> pq;
51             int h=hour-cost[i];
52             for(int j=1;j<=i;j++)
53                 pq.push(p[j]);
54             while(h>0){
55                 pool x=pq.top();
56                 pq.pop();
57                 h--;
58                 sum+=x.gain;
59                 x.time++;
60                 if(x.gain) x.gain-=ji[x.number];
61                if(x.gain<0) x.gain=0;
62                pq.push(x);
63             }
64            if(sum>maxn)
65            {
66                while(!pq.empty())
67                {
68                    pool x=pq.top();
69                    mtime[x.number]=x.time;
70                    pq.pop();
71                }
72                maxn=sum;
73                flag=i;
74 
75            }
76       }
77 
78        for(int i = 1; i <= flag; i ++ ){
79         if(i>1) printf(", ");
80         printf("%d",mtime[i]*5);
81     }
82     for(int i = flag + 1; i <= n; i ++){
83         if(i) printf(", ");
84         printf("0");
85     }
86 
87      printf("\nNumber of fish expected: %d\n",maxn);
88 
89 
90 }
91 
92   return 0;
93 }

 

posted @ 2017-08-24 21:03  余生漫漫浪  阅读(590)  评论(0编辑  收藏  举报