UVA 757 Gone Fishing (greedy)

Source: Internet
Author: User

UVA 757 Gone Fishing


John is going on a fishing trip. He has h hours available (), and there isN lakes in the area () 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, the number of 5-minute intervals it takes-to-travel from Lakei to Lake I + 1 is denoted t< /c4>i (). 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 Lakei, the number of fish expected to being caught in the initial 5 minutes, denotedfi ( ), 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 (). If the number of fish expected to being caught in an interval are less than or equal to Di, there'll be 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.

InputYou'll be given a number of cases in the input. Each case starts with a line containingN. This was followed by a line containingh. Next, there is a line ofNIntegers specifyingFI(), then a line ofNIntegersDI(), and finally, a line ofN-1 integersTI( ). Input is terminated by a case in whichN= 0.OutputFor 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
2110 12 524410 15 20 170 3 4 31 2 34410 15 50 300 3 4 31 2 30
Sample Output
5Number of Fish expected:31240, 0, 0, 0Number of Fish expected:480115, ten, 35Number of fish expected:724



Main topic:

Each set of samples consists of 5 parts:

1) Number of fish ponds n

2) Total time H (hours)

3) n Fish ponds, the yield of the initial fish in each fish pond (number of fish to be caught every 5 minutes)

4) n Fish ponds, the rate at which each fish pond is lost (5 minutes per fish in the fish pond, and the amount of production decreased)

5) Length of journey between N Fishponds (5 minutes)

The maximum number of fish that can be caught in the total time H, and the time spent in each fish pond. When you reach the next fish pond, you cannot return to the previous fish pond.

Problem-solving ideas: First of all, the distance between the fish ponds to deal with the time, to change it from the first fish pond to the second fish pond to the required period.

Enumeration only in the first I fish pond fishing in n cases, so that only in the first I fish pond fishing time = Total time-The first fish pond to the time required for the fish pond, and then in the first I pond to find out the current yield the largest pond, in this fishing, and then repeat the process before, until the first I fish pond can be fished or Time runs out. Thus, the maximum number of n cases is the maximum amount of fish that can be fished.



#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace  Std;int N, H, f[250], d[250], t[250], ans, rec[250], reca[250], temp[250];int find (int x) {int num = 0, id;for (int i = 0; I <= x; i++) {if (temp[i] > num) {num = Temp[i];id = i;}} if (num) {return ID;} else return-1;} int main () {int flag = 0;while (scanf ("%d", &n) = = 1, n) {if (flag) printf ("\ n"); flag = 1;int h;scanf ("%d", &h); H = H * 12;for (int i = 0; i < n; i++) {scanf ("%d", &f[i]);} for (int i = 0; i < n; i++) {scanf ("%d", &d[i]);} T[0] = 0;for (int i = 1; i < n; i++) {scanf ("%d", &t[i]); T[i] + = t[i-1];} memset (RecA, 0, sizeof (RecA)), ans = 0;for (int i = 0; i < n; i++) {//Enumeration N cases: From the first fish pond to the I fish pond int time = H-t[i], sum = 0;i F (Time <= 0) break;memcpy (temp, F, sizeof (f)), memset (REC, 0, sizeof (REC)), while (time) {int id = find (i);//find find the first I fish Fish Pond in Pond if (ID < 0) break;sum + = Temp[id];temp[id]-=d[id];time--;rec[id]++;} if (Time > 0) {//The current I fish pond is no fish can be fished, and there is still time left in the first fish pond rec[0] + + times; if (sum > ans) {ans = sum;memcpy (RecA, REC, sizeof (REC));}} if (ans) {printf ("%d", Reca[0] * 5);} else printf ("%d", H * 5); When there is no fish to catch the time to stay in the first fish pond, sample card here for (int i = 1; i < n; i++) {printf (",%d", Reca[i] * 5);} printf ("\nnumber of fish expected:%d\n", ans); return 0;}



UVA 757 Gone Fishing (greedy)

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.