当前位置: 代码迷 >> 综合 >> 2018北大暑校acm算法训练课程 Gone Fishing 贪心+优先队列
  详细解决方案

2018北大暑校acm算法训练课程 Gone Fishing 贪心+优先队列

热度:67   发布时间:2023-12-28 00:41:20.0

总时间限制: 2000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
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.
样例输入
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
样例输出
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
来源
East Central North America 1999

题意:John要花h小时去钓鱼,他最开始在第一个鱼塘,每次钓鱼需要花费5分钟时间,得到fi条鱼并且鱼塘内鱼的数目会减少di条鱼,他也可以不在这里钓鱼并移动到下一个鱼塘,移动到下一个鱼塘需要花费5*(ti+1)的时间,问他最多钓多少.并算出他在每个鱼塘都呆了多久.

另外如果钓鱼条数相同时,他会选择在编号较小的鱼塘呆更长的时间.

分析:我们可以先假设他只去1,2,3—-t—-n个鱼塘的情况,首先把他们之间的花费减去,这样就相当于他可以在t个鱼塘间瞬间移动了,每次钓鱼多选择最大的获取量,这样使用一个优先队列即可完成

还有一个小坑点就是它有可能钓不到任何鱼,但他还是会花时间在任何池塘中(最大值不要设置成0,不然会wa)

代码如下:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouyestruct p{int val;int d;int t;int i;int cal;friend bool operator < (p t1,p t2){if(t1.val==t2.val)//如果获取相同则选择编号较小的return t1.i>t2.i;return t1.val<t2.val;}
}a[105];int cost[105];
int main(){ios::sync_with_stdio(false);#ifdef Kongxiangzhouyefreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifint n;bool tex=0;while(cin>>n&&n){memset(cost,0,sizeof(cost));int h;cin>>h;h*=60;for(int i=0;i<n;i++){cin>>a[i].val;a[i].i=i;}for(int i=0;i<n;i++)cin>>a[i].d;a[0].t=0;for(int i=1;i<n;i++){cin>>a[i].t;a[i].t*=5;}int ans=-1;//初始值一定要注意for(int i=0;i<n;i++){for(int j=0;j<n;j++)a[j].cal=0;h-=a[i].t;//减去鱼塘间移动的花费int now=h;int val=0;priority_queue<p> q;while(q.size())q.pop();for(int j=0;j<=i;j++)q.push(a[j]);while(now>=5){p add=q.top();q.pop();val+=add.val;a[add.i].cal++;now-=5;add.val-=a[add.i].d;add.val=max(0,add.val);q.push(add);}if(ans<val){
   //计算当前结果的花费ans=val;for(int tem=0;tem<n;tem++)cost[tem]=a[tem].cal*5;}}if(tex)cout<<endl;elsetex=1;for(int i=0;i<n;i++){if(i)cout<<", ";cout<<cost[i];}cout<<endl;cout<<"Number of fish expected: "<<ans<<endl;}return 0;
}
  相关解决方案