Last active
August 29, 2015 14:19
-
-
Save mdtareque/67fc018d7a26b52157fe to your computer and use it in GitHub Desktop.
google codejam 15 - qualification round - problemA - standing ovation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
/** | |
* @author mtk - Mohammed Tareque Khan | |
* More details: http://mtktechiecode.blogspot.in/2015/04/google-codejam-15-standing-ovation.html | |
* | |
* Algo description: | |
* if number of people for next shynesslevel is not zero then: | |
* if totalStandingPeopleYet >= next shynessLevel | |
* then: | |
* All good, carry on, no friend to add | |
* else | |
* add next-shynesslevel - totalStandingPeopleYet number of friend | |
* so that they trigger the next shyness level people to stand up | |
* as the goal is to achieve a complete standing ovation | |
* | |
* */ | |
public class ProblemA { | |
int smax; | |
int friendsAdded; | |
int totalStandingYet; | |
String numOfPeopleAtShynessLevels; | |
public ProblemA() { | |
numOfPeopleAtShynessLevels=""; | |
smax=0; | |
totalStandingYet=0; | |
friendsAdded=0; | |
} | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String s; | |
int caseNo = 1, T = Integer.parseInt(br.readLine()); | |
while(caseNo <= T ){ | |
ProblemA probA = new ProblemA(); | |
s = br.readLine(); | |
probA.smax = Integer.parseInt(s.split(" ")[0]); | |
probA.numOfPeopleAtShynessLevels = s.split(" ")[1]; | |
probA.totalStandingYet=Integer.parseInt(probA.numOfPeopleAtShynessLevels.charAt(0) + ""); | |
// starting for index 1 and considering it as next-shynessLevel | |
for(int nextShynessLevel=1; nextShynessLevel <= probA.smax; nextShynessLevel++) { | |
int numberOfPeopleAtNextShynessLevel = probA.getNumberOfPeopleAtShynessLevel(i); | |
if(numberOfPeopleAtNextShynessLevel != 0) { | |
// System.out.println("Number of people at next level is " + numberOfPeopleAtNextShynessLevel); | |
if(probA.totalStandingYet >= nextShynessLevel) { | |
probA.totalStandingYet += numberOfPeopleAtNextShynessLevel ; | |
} else { | |
int friendsToAdd = nextShynessLevel - probA.totalStandingYet; | |
probA.friendsAdded += friendsToAdd; | |
probA.totalStandingYet += numberOfPeopleAtNextShynessLevel + friendsToAdd; | |
} | |
} | |
} | |
System.out.println("Case #" + caseNo++ +": " + probA.friendsAdded); | |
} | |
} | |
int getNumberOfPeopleAtShynessLevel(int i) { | |
return Integer.parseInt(this.numOfPeopleAtShynessLevels.charAt(i) + ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment