(SPOJ) Supermercado - Solution
Link to the problem: http://br.spoj.com/problems/SUPERMER/
The solution below uses the concept of median to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCase = 0;
String line = br.readLine();
String[] s = line.split(" ");
int numMarkets = Integer.parseInt(s[0]);
while (numMarkets != 0) {
int[] arrayX = new int[numMarkets];
int[] arrayY = new int[numMarkets];
for (int i = 0; i < numMarkets; i++) {
line = br.readLine();
s = line.split(" ");
arrayX[i] = Integer.parseInt(s[0]);
arrayY[i] = Integer.parseInt(s[1]);
}
Arrays.sort(arrayX);
Arrays.sort(arrayY);
int x = 0;
int y = 0;
if (numMarkets%2 == 1) {
x = arrayX[numMarkets/2];
y = arrayY[numMarkets/2];
}
else {
x = (arrayX[numMarkets/2]+arrayX[numMarkets/2-1])/2;
y = (arrayY[numMarkets/2]+arrayY[numMarkets/2-1])/2;
}
System.out.println("Teste " + ++numCase);
System.out.println(x + " " + y + "\n");
line = br.readLine();
s = line.split(" ");
numMarkets = Integer.parseInt(s[0]);
}
br.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
The solution below uses the concept of median to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCase = 0;
String line = br.readLine();
String[] s = line.split(" ");
int numMarkets = Integer.parseInt(s[0]);
while (numMarkets != 0) {
int[] arrayX = new int[numMarkets];
int[] arrayY = new int[numMarkets];
for (int i = 0; i < numMarkets; i++) {
line = br.readLine();
s = line.split(" ");
arrayX[i] = Integer.parseInt(s[0]);
arrayY[i] = Integer.parseInt(s[1]);
}
Arrays.sort(arrayX);
Arrays.sort(arrayY);
int x = 0;
int y = 0;
if (numMarkets%2 == 1) {
x = arrayX[numMarkets/2];
y = arrayY[numMarkets/2];
}
else {
x = (arrayX[numMarkets/2]+arrayX[numMarkets/2-1])/2;
y = (arrayY[numMarkets/2]+arrayY[numMarkets/2-1])/2;
}
System.out.println("Teste " + ++numCase);
System.out.println(x + " " + y + "\n");
line = br.readLine();
s = line.split(" ");
numMarkets = Integer.parseInt(s[0]);
}
br.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment