Poj 1042 gone fishing

Source: Internet
Author: User

This is a standard greedy algorithm. The general meaning of the question is: Now I am giving you some water ponds. Now the child is in the pond 1. Now I am going to fish. At the beginning, Each pond has a fixed number of fish, the number of fish caught every 5 minutes decreases accordingly. When the current number of fish caught in the pond is less than or equal to 0, the number of fish caught each time cannot be caught next time, of course, there is a certain interval between the pond and the pond, this interval is reflected in the time consumed from I to I + 1. Of course, ti * 5 is the real time. Now, I want to give you the time this kid can fish and ask him how many fish he can catch, and ask him to output the time spent in each pond. The practice of this question is that if we know which pond we stop fishing at the end, then of course we will not go back from pond I once, and all the rest of the time will be used for fishing. The problem now is how to arrange the fishing time in these ponds. Of course, we should first choose the pond with the most fish in the first five minutes, then the fish in the pond will lose the number of fish reduced each time, and the next 5 minutes will continue. Note that the output of the question has a requirement, which makes it necessary to calculate the case of the last stuck in the first Lake separately. This question implies that all the time must be exhausted, even if no fish can be caught.

See the program below.

[Cpp]
<SPAN style = "FONT-SIZE: 18px"> # include <iostream>
# Include <stdio. h>
Using namespace std;
Int start [26], change [26], stay [26], compare [26], de [26], dis [26];
Int main ()
{
Int n, h, I, time, sum, used, flag, maxi, j, jilu, sumc;
While (cin> n)
{
If (n = 0)
Break;
Cin> h;
H = h * 60;
For (I = 1; I <= n; I ++)
Scanf ("% d", & start [I]);
Start [0] = 0;
For (I = 1; I <= n; I ++)
Scanf ("% d", & de [I]);
For (I = 1; I <n; I ++)
{
Scanf ("% d", & dis [I]);
Dis [I] = 5 * dis [I];
}
Dis [0] = 0;
For (I = 1; I <= n; I ++)
Stay [I] = 0;
Sum = 0;
Time = h;
Change [1] = start [1];
For (I = 1; I <= n; I ++)
Stay [I] = 0;
While (time> 0)
{
Sum = sum + change [1];
Change [1] = change [1]-de [1];
If (change [1] <0)
Change [1] = 0;
Time = time-5;
Stay [1] = stay [1] + 5;
}
Used = 0;
For (I = 2; I <= n; I ++)
{
Sumc = 0;
Used = used + dis [I-1];
Time = h-used;
Flag = 1;
For (j = 1; j <= n; j ++)
{
Change [j] = start [j];
Compare [j] = 0;
}
While (time> 0)
{
Maxi = 0;
Jilu = 1;
For (j = 1; j <= I; j ++)
{
If (change [j]> maxi)
{
Maxi = change [j];
Jilu = j;
}
}
Sumc = sumc + maxi;
Change [jilu] = change [jilu]-de [jilu];
Compare [jilu] = compare [jilu] + 5;
If (change [jilu] <0)
Change [jilu] = 0;
Time = time-5;
}
If (sumc> sum)
{
Sum = sumc;
For (int k = 1; k <= n; k ++)
Stay [k] = compare [k];
}
}
For (I = 1; I <n; I ++)
Cout <stay [I] <",";
Cout <stay [n] <endl;
Cout <"Number of fish expected:" <sum <endl;
}
Return 0;
}
</SPAN>

# Include <iostream>
# Include <stdio. h>
Using namespace std;
Int start [26], change [26], stay [26], compare [26], de [26], dis [26];
Int main ()
{
Int n, h, I, time, sum, used, flag, maxi, j, jilu, sumc;
While (cin> n)
{
If (n = 0)
Break;
Cin> h;
H = h * 60;
For (I = 1; I <= n; I ++)
Scanf ("% d", & start [I]);
Start [0] = 0;
For (I = 1; I <= n; I ++)
Scanf ("% d", & de [I]);
For (I = 1; I <n; I ++)
{
Scanf ("% d", & dis [I]);
Dis [I] = 5 * dis [I];
}
Dis [0] = 0;
For (I = 1; I <= n; I ++)
Stay [I] = 0;
Sum = 0;
Time = h;
Change [1] = start [1];
For (I = 1; I <= n; I ++)
Stay [I] = 0;
While (time> 0)
{
Sum = sum + change [1];
Change [1] = change [1]-de [1];
If (change [1] <0)
Change [1] = 0;
Time = time-5;
Stay [1] = stay [1] + 5;
}
Used = 0;
For (I = 2; I <= n; I ++)
{
Sumc = 0;
Used = used + dis [I-1];
Time = h-used;
Flag = 1;
For (j = 1; j <= n; j ++)
{
Change [j] = start [j];
Compare [j] = 0;
}
While (time> 0)
{
Maxi = 0;
Jilu = 1;
For (j = 1; j <= I; j ++)
{
If (change [j]> maxi)
{
Maxi = change [j];
Jilu = j;
}
}
Sumc = sumc + maxi;
Change [jilu] = change [jilu]-de [jilu];
Compare [jilu] = compare [jilu] + 5;
If (change [jilu] <0)
Change [jilu] = 0;
Time = time-5;
}
If (sumc> sum)
{
Sum = sumc;
For (int k = 1; k <= n; k ++)
Stay [k] = compare [k];
}
}
For (I = 1; I <n; I ++)
Cout <stay [I] <",";
Cout <stay [n] <endl;
Cout <"Number of fish expected:" <sum <endl;
}
Return 0;
}

 

 

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.