程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ-1042 Gone Fishing (貪心法求最佳釣魚方案

POJ-1042 Gone Fishing (貪心法求最佳釣魚方案

編輯:C++入門知識

Gone Fishing Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 28075 Accepted: 8371

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 


本題在《算法藝術與信息學競賽》一書中被作為貪心法的例題,題目大意如下:

\


解題思路:

還是先放書上的講解,再說說我的理解:

\


接下來補充說明一下:<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+sLTMsNDEt6i1xNSt1PKjrNKqw7+0zra80aHU8cTcubu1w7W91+6088rV0ua1xLr+srTAtLS5tfajrLy0tfbSu7TO0+PWrrrzo6y+zdKq1Nm0ztGw1dLX7tPFtcS6/sewyKW0ubX2o6zV4tH5v7TG8MC0tcS7sKOsyOe6zrzGy+PCt8nPu6i30bXEyrG85L7NysfSu7j2uty+wL3htcTOyszioaPL+dLUo6zO0r71tcOxvsziy7zCt7XE0ru49rnYvPy149Ta09qjur6/vrnQ6NKq1NrCt8nPu6i30bbgydnKsbzkoaM8L3A+CjxwPs2ouf3A/dfTwLTLtcP3oaM8L3A+CjxwPrzZyejP1tTa09AzuPa6/qOsseC6xc6qMaGiMqGiM6Osz9bM4bP2wb3W1rX20+O3vbC4o7o8L3A+CjxwPqOoMaOpMS0+My0+Mi0+My0+MTwvcD4KPHA+o6gyo6kxLT4xLT4yLT4zLT4zPC9wPgo8cD7V4sG91ta3vbC4o6y1pb+0wrfJz7uot9G1xMqxvOSjrM/UyLvKx7e9sLgyuPzTxaGjtvjV4sG91ta3vbC4y/m1w7XE0+PBv8TYo7/X0M+4z+vP66OsysLKtcnPysfDu9PQx/ix8LXEoaPSsr7NysfLtaOs1rvSqsi3tqjBy8Tj1+7UttKq19+1vcTEuPa6/qOssqKwtNXVzLDQxLeotcTUrdTy1/ez9sHLtdrSu9bWt72wuKOsxMfDtM3qyKu/ydLUvavCt8/f16q7u86qt72wuLb+o6zT48G/sru74dPQyM66zsvwyqeho8THw7TE49TawrfJz8v5u6i30bXE19zKsbzkvs3S0b6tyLe2qMHLo6zSsr7NysfSu8z11rHP39fftb3X7rrzy/nQ6LXEyrG85KGjPC9wPgo8cD48YnI+CjwvcD4KPHA+UHOjurbUsb7M4qOsyPTL+dPQuv62vNLRvq2/1cHLo6yyosfSyrG85Lu509DKo9Pgo6zErMjP0qqw0cqj0+DKsbzkyKuyv7zGyOu12tK7uPa6/rXEyrG85KGjPC9wPgo8cD48YnI+CjwvcD4KPHA+tPrC66O6PC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">#include #include #include using namespace std; int ans[30][30],f[30],d[30],t[30]; int main() { int h,n; cin>>n; while(true) { if(n==0) break; cin>>h; h*=12; memset(ans,0,sizeof(ans)); memset(f,0,sizeof(f)); memset(t,0,sizeof(t)); memset(d,0,sizeof(d)); for(int i=1;i<=n;++i) { scanf("%d",&f[i]); } for(int i=1;i<=n;++i) { scanf("%d",&d[i]); } for(int i=1;i0&&emp<=ed)//時間用完或湖空為止 { k=1; for(int j=emp;j<=ed;++j) { if(ft[j]>ft[k]) { k=j; } }//找出最優的湖 ans[ed][0]+=ft[k];//此次收獲+ft[k] ++ans[ed][k];//記錄在k湖花費了1單位時間 --ht;//時間消耗1單位 ft[k]-=d[k]; ft[k]=ft[k]>0?ft[k]:0; for(int j=emp;j<=ed;++j) { if(ft[j]==0) ++emp; else break; }//檢查是否ed前的湖都已空 } if(ht>0) ans[ed][1]+=ht;//若時間有剩余 } int a=1; for(int i=2;i<=n;++i) { if(ans[i][0]>ans[a][0]) a=i; }//找出收益最大的方案 for(int i=1;i<=n;++i) { cout<>n; if(n!=0) cout<


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved