Reputation: 41
I am working on a program designed to solve a challenge problem located at the following site. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=919
I believe I have correctly solved the problem. I need to print out my answers in a specific fashion. For almost all test input my program prints out the response in the correct fashion. However, for the input given by the following site I get one error. http://www.udebug.com/UVa/978
I am missing a newline character around line 137383.
I should have:
1
1
1
green wins
I get:
1
1
1
green wins
I have run combed through my code and cannot figure out why this occurs. It does not appear to happen anywhere else, even for similar input.
Here is the code:
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Lem4 {
static Comparator<Integer> c = new reverseC();
static PriorityQueue<Integer> green = new PriorityQueue<Integer>(100002, c);
static PriorityQueue<Integer> blue = new PriorityQueue<Integer>(100002, c);
static int bf[][] = new int[100048][2];
public static void main(String[] args) {
Scanner in = new Scanner((System.in));
int count = in.nextInt();
StringBuilder t = new StringBuilder();
while (count-- > 0) {
int fields = in.nextInt();
int numGreen = in.nextInt(); // SG
int numBlue = in.nextInt(); // SB
for (int i = 0; i < numGreen; i++) {
green.add(in.nextInt());
}
for (int i = 0; i < numBlue; i++) {
blue.add(in.nextInt());
}
while (!green.isEmpty() && !blue.isEmpty()) {
int battles = 0;
int diff = 0;
while (!green.isEmpty() && !blue.isEmpty() && battles < fields) {
bf[battles][0] = green.peek();
green.poll();
bf[battles][1] = blue.peek();
blue.poll();
++battles;
}
for (int i = 0; i < battles; ++i) {
diff = bf[i][0] - bf[i][1];
if (diff > 0)
green.add(diff);
else if (diff < 0)
blue.add(-1 * diff);
}
}
if (!green.isEmpty()) {
t.append("green wins" + "\n");
while (!green.isEmpty()) {
t.append(green.peek());
t.append('\n');
green.poll();
}
} else if (!blue.isEmpty()) {
t.append("blue wins" + "\n");
while (!blue.isEmpty()) {
t.append(blue.peek());
t.append('\n');
blue.poll();
}
} else {
t.append("green and blue died\n");
}
if (count > 1)
t.append("\n");
}
System.out.print(t);
}
public static class reverseC implements Comparator<Integer> {
@Override
public int compare(Integer x, Integer y) {
if (x > y)
return -1;
else if (x < y)
return 1;
else
return 0;
}
}
}
Does anyone have any clue what is going on here?
Upvotes: 0
Views: 316
Reputation: 41
Nevermind. I figured it out. My count statement:
if (count > 1) {
t.append("\n");
}
Should have been:
if (count > 0) {
t.append("\n");
}
I fixed that and it works like a charm.
Upvotes: 1