Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve in Java or C + + , thank you. : ) Farmer John has N ( 1 N 2 * 1 0 5

Please solve in Java or C++, thank you. :)
Farmer John has N(1N2*105) patches of grass in a line, where patch i has a level of bacteria that differs by ai from that
of healthy grass (-1015ai1015). For example, if ai=-3, then patch i has a level of bacteria 3 lower than normal, and would
need exactly 3 additional units of bacteria added to raise it to the point where it is considered healthy.
Farmer John wants to ensure every patch of grass is corrected to have a healthy level of bacteria. Conveniently, he owns two
brands of pesticide that he can spray on his field, one that adds bacteria and one that removes bacteria. When Farmer John
sprays either type of pesticide, he stands in patch N(the rightmost patch) and selects a power level L for his sprayer (
1LN.
The sprayer has the most impact on patches near Farmer John, with diminishing effect farther away. If Farmer John chooses the
pesticide that adds bacteria, then L units of bacteria will be added to patch N,L-1 units to patch N-1,L-2 units to patch
N-2, and so on. Patches 1dotsN-L will receive no bacteria, since the sprayer isn't set to a level powerful enough to reach
them. Similarly, if Farmer John chooses the pesticide that removes bacteria, then L units of bacteria will be removed from patch
N,L-1 units will be removed from patch N-1, and so on. Again, patches 1dotsN-L will be unaffected.
Find the minimum number of times Farmer John has to apply his sprayer such that every patch of grass has the recommended
value of bacteria for healthy grass. It is guaranteed that the answer is at most 109.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a
"long long" in CC++.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains N.
The second line contains N integers a1dotsaN, the initial bacteria levels of each patch of grass.
OUTPUT FORMAT (print output to the terminal / stdout):
The minimum number of applications necessary to make every patch of grass have the recommended value of bacteria for
healthy grass.
SAMPLE INPUT:
2
-13
SAMPLE OUTPUT:
6
Use the type of pesticide that removes bacteria, at a power level of 1, five times. Then use the type of pesticide that adds
bacteria, with a power level of 2, one time.
SAMPLE INPUT:
5
1,3,-2,-7,5
SAMPLE OUTPUT:
26
SCORING:
Inputs 3-5: N103, the answer is at most 103
Inputs 6-10: N103
Inputs 11-15: No additional constraints.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

3. What should a contract of employment contain?

Answered: 1 week ago

Question

1. What does the term employment relationship mean?

Answered: 1 week ago