POJ 1042 Gone Fishing (greedy) (Rujia Black Book)

Source: Internet
Author: User

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

field=source&key=east+central+north+america+1999 ">east Central North America 1999



Analysis: Because it is from the first lake. And all the lakes are lined up, so just enumerate the number of lakes he walks through X. That is, first if he walks from Lake 1 to Lake X. The road spent a total of t= T1 + T2 + T3 + ... + Tx. Under this premise. Can feel that he has the ability to "teleport" between two lakes between 1~x. That is, at any one time can choose a 1~x in the lake fishing.

(Think about why?) In fact, this is the same reason the car refueling, in every lake fishing order can not come in turn, you may feel that the total time is certainly more than this. In fact, the order does not affect the result. Because if I had to go to Lake 1 for 5 minutes, then go to the Lake 2 fishing for 5 minutes. Then come back to the Lake 1 fishing for 5 minutes, the process is actually equivalent to first in the Lake 1 fishing 5+5=10 minutes, and then go to the Lake 2 fishing for 5 minutes. So just have to be greedy to choose the current can catch the most fish lake can. There is also the time to choose greed. If you have the same lake, prioritize the lake with the smaller number.




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 every 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", &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.