Algo1 Assign7
Algo1 Assign7
ASSIGNMENT 7
Date: 7th Oct, 2021
Important Instructions
1. Input: To be taken from stdin
2. Format of files to be submitted to Moodle and HackerRank: <ROLLNO> A7 p1.c(pp),
<ROLLNO> A7 p2.c(pp)
3. You are to stick to the file input output formats strictly as per the instructions.
4. Write your name and roll number at the beginning of your program.
5. Do not use any global variable unless you are explicitly instructed so.
6. Indent and comment your code properly.
7. Please follow all the guidelines. Failing to do so will result in penalty.
8. There will be partial markings.
Greedy Algorithms
You are the emperor/empress of the Malazan Empire. You have successfully unified the Lufthanian
continent, but the Elves (also known as the forest folks) living in the forest have successfully eluded
your conquest numerous times, and are now organising a campaign against your unified Empire. You
obviously think nothing of their last ditch effort, but you still want to minimize the casualties. With your
omnipotent foresight, you found out that their target is Itno Kahn, a fortress city that is cornerstone in
maintaining the stability of your Empire.
B You are given N food samples. You have to taste them one by one, in order. You can skip any of
them you want, but you cannot come back and taste it if you have skipped it. You have knowledge
of all the food samples. So, you can make decisions for the current food sample (whether to eat
or not) based on the food samples that come ahead.
B You are also given how much the poison in the sample affects you. For example:- if a food sample
has a value of 3, it means it heals you for 3 health, whereas a value of −9 indicates a damage of 9
health. A value of 0 represents no effect.
1
B You start with X health. Obviously while tasting, you cannot let your health reach or go below 0.
What is the maximum number of food samples can you taste?
Input Format
B The first line contains a single integer T (1 6 T 6 100), indicating the number of test cases.
B Each test case is made up of 2 lines. In the first line are two integers N(1 6 N 6 2000) and
X(1 6 X 6 109 ), the number of food samples and the initial health, respectively. The next line
consists of N space separated integers Ai (−109 6 X 6 109 ), the amount the ith food sample affects
you.
B It is guaranteed that the sum of N across all test cases is less than 2500.
Output Format
For each of the test case, output a single integer (the maximum number of food instances you can
taste) in a separate line.
Sample Scenario
F ILE : input-part1.txt
3
5 4
2 -8 -6 1 -6
Input 5 6
2 -5 -2 -2 -2
6 1
2 0 -1 5 0 -1
F ILE : output-part1.txt
3
Output 4
6
Explanation
B In test case 1, after eating the 1st food sample, your health is 6. If you eat the 2nd food sample,
your health will go down to -2, which signifies your death. So you have to skip it. The 3rd food
sample will bring down your health to 0, so you have to skip that as well. After eating the 4th
food sample, your health is 7 and hence you can eat the 5th food sample, which will leave you at
1 health. Therefore, you can taste a maximum of 3 food samples.
2
B In test case 2, the maximum food sample testing occurs when you try the 1st, 3rd, 4th and 5th
sample.
B In test case 3, there is no problem even when you try all the food samples.
Input Format
B The first line contains a single integer T (1 6 T 6 100), indicating the number of test cases.
B Each test case is made up of 2 lines. In the first line are three integers N(1 6 N 6 105 ), X(0 6 X 6
1018 ) and K(0 6 K < N), indicating the number of battlements, the number of reserve archers
and the range they can cover, respectively. The next line consists of N space separated integers
Ai (0 6 X 6 109 ), the number of soldiers initially in battlement i.
B It is guaranteed that the sum of N across all test cases is less than 105 .
Output Format
For each of the test case, output a single integer (the maximum strength of the wall you can achieve)
in a separate line.
Sample Scenario
F ILE : input-part2.txt
4
5 0 2
4 2 5 3 1
5 4 0
Input 1 4 2 5 3
5 6 0
1 4 2 5 3
5 2 1
2 4 1 1 2
3
F ILE : output-part2.txt
9
3
Output
4
5
Explanation
B In test case 1, you do not have any reserve archers. So the current strength of the wall is the
answer. A detailed observation will show that the number of archers covering each battlement is
M = [11, 14, 15, 11, 9], with K = 2. So, the strength of the wall is 9 as that is the minimum of all
the battlements.
B In test case 2, you have 4 reserve archers, but each archer can cover only their battlement. So
here, M = [1, 4, 2, 5, 3]. To increase the strength of the wall, we need to add archers in such a way
that they increase the minimum values of M. After optimally adding archers, M = [3, 4, 3, 5, 4].
Therefore, the strength of the wall is 3.
B In test case 3, we have a similar case, but we have 2 more reserve archers, which will result in the
final cover array as M = [4, 4, 4, 5, 4], which makes answer 4.
B In test case 4, the cover array M = [6, 7, 6, 4, 3]. You have 2 reserve archers, where each archer
has a range of 1. With that if we want to increase the minimum value in M, it will result in
M = [6, 7, 8, 6, 5] when we add both the reserve archers to the 4th battlement. It can be proved
that this is an optimal assignment and hence the answer is 5.