Ultraviolet a 757-gone fishing

Source: Internet
Author: User

[Question link]

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 698

[Original question]

John is going on a fishing trip. He hasHHours available (),
And there areNLakes 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 have to stop at any lake unless he wishes to. For each,
The number of 5-minute intervals it takes to travel from LakeITo LakeI+ 1 is denotedTI().
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 lakeI, The number of fish expected to be caught in the initial 5 minutes, denotedFI(),
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 rateDI().
If the number of fish expected to be caught in an interval is less than or equalDI, 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
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(),
Then a line of N integers dI(),
And finally, a line of n-1 integers tI().
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 shocould 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 has Ds 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 has Ds 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

45, 5Number of fish expected: 31240, 0, 0, 0Number of fish expected: 480115, 10, 50, 35Number of fish expected: 724

[Analysis and Summary]

A greedy Question of the Super classic, the first example of the greedy chapter in the lrj black book.



I implemented it using heap.


[Code]

/** Ultraviolet A: 757-gone fishing * greedy * Time: 0.044 S (ultraviolet A), 79 Ms (poj) * Author: d_double **/# include <iostream> # include <cstring> # include <cstdio> # include <queue> # define maxn 30 using namespace STD; int H, N; int ans [maxn], TMP [maxn]; struct node {int no; // the nth Lake int rate; // The fish int down every 5 minutes; // int time for the number of phishing scams reduced every five minutes; // The time from the first Lake to the current Lake friend bool operator <(const node & A, const node & B) {if (. rate! = B. rate) return. rate <B. rate; return. no> B. no; // if the fish caught every five minutes are the same, the minor lake will be caught first} arr [maxn]; priority_queue <node> que; void greedy () {int maxsum =-10000; For (INT I = 0; I <n; ++ I) {// enumeration Fishing 1 ~ While (! Que. empty () que. pop (); For (Int J = 0; j <= I; ++ J) que. push (ARR [J]); int lefttime = H * 60-arr [I]. time, sum = 0; memset (TMP, 0, sizeof (TMP); While (lefttime> 0) {node temp = que. top (); que. pop (); If (temp. rate <= 0) break; sum + = temp. rate; temp. rate-= temp. down; TMP [temp. no] + = 5; que. push (temp); lefttime-= 5;} If (lefttime> 0) TMP [0] + = lefttime; // Add the remaining time to the first lake if (sum> maxsum) {maxsum = sum; For (Int J = 0; J <n; ++ J) ans [J] = TMP [J] ;}} printf ("% d", ANS [0]); For (INT I = 1; I <n; ++ I) printf (", % d", ANS [I]); printf ("\ n"); printf ("number of fish expected: % d \ n ", maxsum);} int main () {bool flag = false; while (~ Scanf ("% d", & N) {scanf ("% d", & H); For (INT I = 0; I <N; ++ I) {scanf ("% d", & arr [I]. rate); arr [I]. no = I ;}for (INT I = 0; I <n; ++ I) {scanf ("% d", & arr [I]. down);} arr [0]. time = 0; For (INT I = 1; I <= n-1; ++ I) {int t; scanf ("% d", & T); arr [I]. time = arr [I-1]. time + T * 5;} If (FLAG) printf ("\ n"); else flag = true; greedy ();} return 0 ;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

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.