Gone Fishing
Time Limit: 2000MS |
|
Memory Limit: 32768K |
Total Submissions: 30281 |
|
Accepted: 9124 |
Description
John is going on a fishing trip. He has h hours available (1 <= h <=), and there is N lakes in the area (2 <= n <=) 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 has 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 & Lt Ti <=192). For example, T3 = 4 means the It takes minutes to travel from Lake 3 to Lake 4. To help plan he fishing trip, John had gathered some information about the lakes. For each lake I, the number of fish expected to being caught in the initial 5 minutes, denoted Fi (fi >= 0), is known. Each 5 minutes of fishing decreases the number of fish expected to being caught in the next 5-minute interval by a constant R Ate of Di (di >= 0). If the number of fish expected to being caught in an interval are less than OR equal to Di, there'll be is no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else would be a fishing at the lakes to affect the number of fish he expec TS to catch.
Write a program to help John plan his fishing trips to maximize the number of fish expected to be caught. The number of minutes spent at each lake must is a multiple of 5.
Input
You'll be given a number of cases in the input. Each case is starts with a line containing N. This was 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, and for the plan achieving the Maximu M number of fish expected to is caught (you should print the entire plan on one line even if it exceeds characters). This was followed by a line containing the number of fish expected.
If multiple plans exist, choose the one that is spends as long as possible at Lake 1, even if no fish is expected to being Caug HT in some intervals. If There is still a tie, choose the one spends as long as possible at Lake 2, and so on. Insert a blank line between cases.
Sample Input
Sample Output
Source
East Central North America 1999
Resolution: Because it is from the first lake, and all the lake is a word line, so just enumerate the number of lakes he walked X can. Assuming he walked from Lake 1 to Lake X, the road spent a total of t= T1 + T2 + T3 + ... + Tx. Under this premise, it can be thought that he has the ability to "teleport" between any two lakes in the 1~x, that is, at any one time can choose a 1~x lake fishing. (Think about why?) In fact, this is the same reason that the car refueling, the fishing order in each lake can not come in turn, you may think the total time is certainly more than this spend, actually not, the order actually does not affect the result. Because if I want to first go to Lake 1 fishing 5 minutes, then go to Lake 2 fishing 5 minutes, then come back Lake 1 fishing 5 minutes, this process is actually equivalent to first in the Lake 1 fishing 5+5=10 minutes, and then go to Lake 2 fishing 5 minutes). So just always greedy to choose the most fish can now catch the lake can. There is the greedy choice, if there is the same lake, priority to choose the smaller number of the lake.
AC Code:
#include <algorithm> #include <queue> #include <iostream> #include <cstdio> #include < Cstdlib> #include <cstring>using namespace std;const int maxn = 30;int T[MAXN], F[MAXN], d[maxn];struct node{i NT ID; int F; int D; friend bool Operator < (Node A, node B) {//note from large to small rows, to reload ' < ' if (A.F = = B.F) return a.id > b.id; If the number of fish is equal, select return A.F < B.F with a smaller ID; }};node Fish[maxn];int TIMES[MAXN][MAXN]; Record each lake fishing time int main () {#ifdef sxk freopen ("In.txt", "R", stdin); #endif//SXK int n, h; while (scanf ("%d", &n)!=eof && N) {scanf ("%d", &h); Memset (times, 0, sizeof (times)); H = h * 12; for (int i=1; i<=n; i++) {scanf ("%d", &fish[i].f); fish[i].id = i;} for (int i=1; i<=n; i++) scanf ("%d", &FISH[I].D); for (int i=1; i<=n-1; i++) scanf ("%d", &t[i]); int Maxans = 0; int maxk = 1; for (int i=1; i<=n; i++) { int TC = 0; for (int j=1; j<i; j + +) TC + T[J]; Priority_queue<node> p; for (int j=1; j<=i; j + +) P.push (Fish[j]); The amount of fish in the lake 1~x is placed in the priority queue from the big to the small row int ans = 0; int t = H-TC; for (int j=1; j<=t; J + +) {node Foo = p.top (); Ans + = FOO.F; Times[i][foo.id] + = 5; P.pop (); P.push (Node{foo.id, Max (FOO.F-FOO.D, 0), foo.d}); } if (Maxans < ans) {Maxans = ans; Maxk = i; }} for (int i=1; i<n; i++) printf ("%d,", times[maxk][i]); printf ("%d\n", Times[maxk][n]); printf ("Number of fish expected:%d\n\n", Maxans); } return 0;}
POJ 1042 Gone Fishing (greedy) (Rujia Black Book)