Question

C Programming Language Problem Title : Magical Cave Lili, a great magician, has a mission to...

C Programming Language

Problem Title : Magical Cave

Lili, a great magician, has a mission to enter a cave to get treasure inside. The cave only has 1 path without branches. But the cave is not safe because there are some traps inside that can reduce Lili’s life points. But in addition to traps, the cave also has potions that can increase Lili’s life points. Before entering the cave, Lili casts magic that can reveal all the traps and potions inside the cave. But before entering the cave, Lili must prepare her life points first because in the cave because Lili cannot use her magic to add life points or destroy the traps. What is the minimum life point that Lili must prepare so that her life point is always positive during the trip inside the cave.

Note: if Lili's point drops to 0 or negative before entering and during the trip inside the cave, then Lili is declared dead.

Format Input

There are T test cases. Each testcase contains an integer N which represents the length of the cave. On the next line there are N numbers represents the value of trap and potion. Traps are marked with numbers that are negative and potions are marked with numbers that are positive.

Format Output

Output T line with format “Case #X: ”, where X represents the testcase number and Y represents the initial life points that Lili has to prepare.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 5000
  • −108 ≤ Ai ≤ 108, which Ai is the value of each traps and potions.

Sample Input & Output (standard input & output)

2
5
1 2 -3 4 -5
Case #1: 2
5
-1 -1 -1 -2 9
Case #2: 6

Explanation
In case 1, the minimum life points that Lili must prepare is 2. With a simulation like the following.
At position 1, Lili’s life point increased by 1 to 3.
At position 2, Lili’s life point increased by 2 to 5.
At position 3, Lili’s life point is reduced by 3 to 2.
At position 4, Lili’s life point increased to 4 to 6.
At position 5, Lili’s life point is reduced by 5 to 1.
In each position Lili’s life points are Positive so the answer is Valid. if the initial life
prepared by Lili is 1, then Lili will die in fifth position with a life point of 0.

Homework Answers

Answer #1

The problem can be solved using greedy approach. Answer can be calculating by finding the minimum sum.
If the sum > 0 , then no need to start with any minimum life , otherwise starting life will be -minSum+1.

Here is the C code for the problem:

#include<stdio.h>

long calculateMin(long a ,long b){
        if(a<=b)
                return a;
        return b;
}
int main(){
        int t;
        int tc =0;
        scanf("%d",&t);
        while(t--){
                tc++;
                int n;
                scanf("%d",&n);
                long minSum = 100000008;
                long sm = 0;
                int i= 0 ;
                for(i;i<n;i++){
                        int num;
                        scanf("%d",&num);
                        sm+=(long)num;
                        minSum = calculateMin(minSum,sm);
                }
                if(minSum>0){
                        printf("Case #%d: 0\n",tc);
                }else{
                        long ans = -minSum+1;
                        printf("Case #%d: %ld\n",tc,ans );
                }
        }
}

Here is the screenshot of Input and Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C Programming Language Problem Title : Container Today is Jojo’s birthday. To prepare the birthday party,...
C Programming Language Problem Title : Container Today is Jojo’s birthday. To prepare the birthday party, Jojo asks Bibi to bring N cubes with edge length S for the game on his birthday party. The only idea that came up to Bibi’s mind is to bring the cubes with rectangular box containers. Then Bibi went to a store. The only container available in the store is a container with size A × B × C. Bibi is a thrifty person....
C Programming Language Problem Title : Promotion Jojo is at the supermarket buying monthly groceries. As...
C Programming Language Problem Title : Promotion Jojo is at the supermarket buying monthly groceries. As he was passing alley by alley, a certain banner caught his interest. Buy K cans of cola and get 1 free while one can of cola costs D dollars. As an avid cola fan, Jojo wouldn’t want to miss this amazing opportunity. Jojo is bad at math and so he asks you to count the price that he needs to pay if he plans...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
Mastery Problem: CVP Analysis - Constructing a Cost-Volume-Profit Chart CVP Analysis and the Contribution Margin Income...
Mastery Problem: CVP Analysis - Constructing a Cost-Volume-Profit Chart CVP Analysis and the Contribution Margin Income Statement For planning and control purposes, managers have a powerful tool known as cost-volume-profit (CVP) analysis. CVP analysis shows how revenues, expenses, and profits behave as volume changes, which helps identify problems and create solutions. In CVP analysis, costs are classified according to behavior: variable or fixed, rather than by category: product (which includes both variable and fixed) or period (which includes both variable...
Mastery Problem: CVP Analysis - Constructing a Cost-Volume-Profit Chart CVP Analysis and the Contribution Margin Income...
Mastery Problem: CVP Analysis - Constructing a Cost-Volume-Profit Chart CVP Analysis and the Contribution Margin Income Statement For planning and control purposes, managers have a powerful tool known as cost-volume-profit (CVP) analysis. CVP analysis shows how revenues, expenses, and profits behave as volume changes, which helps identify problems and create solutions. In CVP analysis, costs are classified according to behavior: variable or fixed, rather than by category: product (which includes both variable and fixed) or period (which includes both variable...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s wants and needs for goods and services seem to be unlimited. (2 points) 2.) (1 point) Adam Smith’s “invisible hand” refers to a.) the subtle and often hidden methods that businesses use to profit at consumers’ expense. b.) the ability of free markets to reach desirable outcomes, despite the self-interest of market participants. c.) the ability of government regulations to benefit consumers, even if...
just do questions 5 through 10 3.13.6 Question 110 pts A 319 kg motorcycle is parked...
just do questions 5 through 10 3.13.6 Question 110 pts A 319 kg motorcycle is parked in a parking garage. If the car has 35,494 J of potential energy, how many meters above ground is the car? Report your answer to 1 decimal place. Please do not include units or the answer will be marked incorrect. Flag this Question Question 210 pts A box sitting on the top of a hill has 252 J of potential energy. If the hill...
8 through 10 done please!! 3.13.6 Question 110 pts A 319 kg motorcycle is parked in...
8 through 10 done please!! 3.13.6 Question 110 pts A 319 kg motorcycle is parked in a parking garage. If the car has 35,494 J of potential energy, how many meters above ground is the car? Report your answer to 1 decimal place. Please do not include units or the answer will be marked incorrect. Flag this Question Question 210 pts A box sitting on the top of a hill has 252 J of potential energy. If the hill is...
answer all questions 3.13.6 Question 110 pts A 319 kg motorcycle is parked in a parking...
answer all questions 3.13.6 Question 110 pts A 319 kg motorcycle is parked in a parking garage. If the car has 35,494 J of potential energy, how many meters above ground is the car? Report your answer to 1 decimal place. Please do not include units or the answer will be marked incorrect. Flag this Question Question 210 pts A box sitting on the top of a hill has 252 J of potential energy. If the hill is 279 meters...