0% found this document useful (0 votes)
7 views

Algo1 Assign7

Uploaded by

xahage9739
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Algo1 Assign7

Uploaded by

xahage9739
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS29003 ALGORITHMS LABORATORY

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.

Part 1: The Poison! (40 marks)


While supervising the east side of the fortress, you realised there is an anomaly in the food supply.
After investigating, you found out that the food is poisoned. Unfortunately, the elves being loved by
nature, were able to concoct a poison that is very difficult to cure. But it still can’t stop you, the first Arch
Mage of the continent. To figure out the antidote though, you need to taste test as much of the poison
you can out of the samples given to you. Because of your abundant mana, the poison behaves erratically
on you. Sometimes it heals you, and sometimes it damages you.

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?

You have to solve multiple such instances.

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.

Part 2: Defend the Fortress (60 marks)


Now that you have thwarted their dishonest method to poison your army, they have decided to mount
a straight up assault at the south wall. So you need to strengthen your defenses at that wall by placing
archers along it. Imagine the fortress wall as an 1D axis. There are N battlements (places where archers
can shoot from) in the fortress wall. Each battlement is wide enough to accommodate as many archers
you assign (the capacity is infinite). An archer at battlement i, can cover (shoot arrows) all battlements
in the closed range of [max(1, i − K), min(N, i + K)]. Initially, there are certain number of archers present
in each battlement and you have X reserve archers in your castle. Let the number of archers covering
battlement i be Mi . The strength of the wall is min Mi . You want to increase the strength of the wall
16i6N
by allotting the reserve soldiers to the battlements in the wall. Find the maximum strength of the wall
you can achieve.
You have to solve multiple such instances.

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.

You might also like