Created
March 23, 2018 09:09
-
-
Save ignaciobll/167a8843c0738ef90f6ec0893d39b671 to your computer and use it in GitHub Desktop.
Ada Byron Fase Local UPM - Problema 591 - Box of Bricks
This file contains hidden or 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
// https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
// Imports solo para leer decentemente. | |
// Por algo amamos Java para aprender a programar (no). | |
class Main { | |
static class FastReader { | |
BufferedReader br; | |
StringTokenizer st; | |
public FastReader() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next(){ | |
while (st == null || !st.hasMoreElements()){ | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
System.out.println("Error " + e.getMessage()); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt(){ | |
return Integer.parseInt(next()); | |
} | |
long nextLong(){ | |
return Long.parseLong(next()); | |
} | |
double nextDouble(){ | |
return Double.parseDouble(next()); | |
} | |
String nextLine(){ | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} | |
catch (IOException e) { | |
return null; | |
} | |
return str; | |
} | |
} | |
public static void main (String [] args) { | |
FastReader fr = new FastReader(); | |
int n_set = 1; | |
for (int n = fr.nextInt(); n > 0; n = fr.nextInt()){ | |
System.out.println("Set #" + n_set); | |
// Lectura de los tamaños de las pilas | |
int [] stacks = new int [n]; | |
int total = 0; // Número total de ladrillos | |
int bricks = 0; | |
for (int i = 0; i < n; i++){ | |
bricks = fr.nextInt(); | |
stacks[i] = bricks; | |
total += bricks; | |
} | |
// Condición: el número total de ladrillos es divisible por el número de stacks | |
int brsXstack = total / n ; // n es el número de pilas | |
for (int i = 0; i < n; i++) | |
stacks[i] = ((stacks[i] - brsXstack) > 0)? stacks[i] - brsXstack : 0; | |
int moves = 0; | |
for (int brs : stacks) | |
moves += brs; | |
System.out.println("The minimum number of moves is " + moves + "."); | |
System.out.println(""); | |
n_set++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment