Skip to content

Instantly share code, notes, and snippets.

@alcedo
Last active December 22, 2015 03:19
Show Gist options
  • Select an option

  • Save alcedo/6409794 to your computer and use it in GitHub Desktop.

Select an option

Save alcedo/6409794 to your computer and use it in GitHub Desktop.
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
import static java.lang.System.out;
/**
* Main mistake: doing a foreach loop in a priority queue DOES NOT GURANTEES sorted order :( one would have to continuously poll and output
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedOutputStream output = new BufferedOutputStream(System.out);
InputReader input = new InputReader(System.in);
int testCases = input.readInt();
while(testCases-- != 0) {
int battles = input.readInt();
int green = input.readInt();
int blue = input.readInt();
// get green army
PriorityQueue<Integer> gArmy = new PriorityQueue<Integer>();
for(int i=0; i<green; i++) {
gArmy.add(input.readInt() * -1);
}
// get blue army
PriorityQueue<Integer> bArmy = new PriorityQueue<Integer>();
for(int i=0; i<blue; i++) {
bArmy.add(input.readInt() * -1);
}
// for(int x : gArmy)
// out.println("t: " + testCases + " g: " + x + " cnt: " + gArmy.size());
//
// for(int x : bArmy)
// out.println("t: " + testCases + " b: " + x);
ArrayList<Integer> gSurvive = new ArrayList<Integer>(green);
ArrayList<Integer> bSurvive = new ArrayList<Integer>(blue);
while(!gArmy.isEmpty() && !bArmy.isEmpty()) {
for(int i=0; i<battles; i++) { // field army for each battle field
int g=0, b=0;
if(!gArmy.isEmpty())
g = gArmy.poll() * -1;
if(!bArmy.isEmpty())
b = bArmy.poll() * -1;
int outcome = g - b;
if(outcome > 0) { // green won
gSurvive.add(outcome);
}else if (outcome < 0) { //blue won
bSurvive.add(outcome * -1);
} // else both died
}
// add back survivors
for(int x : gSurvive)
gArmy.add(x * -1);
for(int x : bSurvive)
bArmy.add(x * -1);
gSurvive.clear();
bSurvive.clear();
}
StringBuilder sb = new StringBuilder(250000);
if(gArmy.isEmpty() && bArmy.isEmpty()){
sb.append("green and blue died\n");
}
else if(!gArmy.isEmpty() && bArmy.isEmpty()) {
sb.append("green wins\n");
while(!gArmy.isEmpty())
sb.append( gArmy.poll() * -1 + "\n" );
}else if(gArmy.isEmpty() && !bArmy.isEmpty()){
sb.append("blue wins\n");
while(!bArmy.isEmpty())
sb.append( bArmy.poll() * -1 + "\n" );
}
if(testCases > 0)
sb.append("\n");
output.write(sb.toString().getBytes());
}
//output.write(sb.toString().getBytes());
output.flush(); // flush and display
output.close();
}//end of void main
}//end of class main
//BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
/*
Slightly less efficient
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
output.write(stringBuilder.toString());
More efficient:
BufferedOutputStream output = new BufferedOutputStream(System.out); // more efficient
output.write(stringBuilder.toString().getBytes());
*/
//or we can use the below utilitiy classes:
/**
* Various utility class for Fast I/O in java.
* Readings: http://www.codechef.com/wiki/java#IO_in_Java
* BufferedInputStream > BufferedReader > Scanner
*
* USAGE:
* InputReader in = new InputReader(System.in);
* OutputWriter out = new OutputWriter(System.out);
*
* int i = in.readInt(); //read int
* String s = in.readString(); //read string
* int[] x = IOUtils.readIntArray(in,N); //read int array of size N
* String s = in.readLine() // read a line ( to be used with tokenizer );
*
* out.printLine("X");
*
* out.flush(); // flush output
* out.close(); // remember to close the outputstream, at the end (might be able to not do this to save some time)
*/
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readLine() {
int c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (c != '\n');
return res.toString();
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment