(URI) Estiagem - Solução
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
class Main {
public static int sumX;
public static int sumY;
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void readEntries(BufferedReader br, Consumption[] list, int n) throws NumberFormatException, IOException {
for (int i = 0; i < n; i++) {
int x = reader(br);
int y = reader(br);
sumX += x;
sumY += y;
Consumption c = new Consumption(x, y/x);
list[i] = c;
}
}
public static int checkAverage(Consumption[] list, Consumption[] list2, int n) {
int index = 0;
for (int i = 0; i < n; i++) {
boolean averageAdded = false;
if (list[i].x != -1) {
for (int j = i+1; j < n; j++) {
if (list[i].average != list[j].average) {
break;
}
else {
if (!averageAdded) {
Consumption c = new Consumption(list[i].x+list[j].x, list[i].average);
list2[index] = c;
averageAdded = true;
}
else {
Consumption c = new Consumption(list2[index].x+list[j].x, list2[index].average);
list2[index] = c;
}
Consumption tmp = new Consumption(-1, -1);
list[j] = tmp;
}
}
if (!averageAdded) { // if there was not any other slot equal do the current slot
Consumption c = new Consumption(list[i].x, list[i].average);
list2[index] = c;
}
index++;
}
}
return index;
}
public static void printAnswer(Consumption[] list2, int length, int count) {
System.out.println("Cidade# " + count + ":");
for (int i = 0; i < length-1; i++) {
System.out.print(list2[i].x + "-" + list2[i].average + " ");
}
System.out.println(list2[length-1].x + "-" + list2[length-1].average);
DecimalFormat df = new DecimalFormat("#.00");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("Consumo medio: " + df.format((double)sumY/sumX) + " m3.");
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = 0;
int n = reader(br);
while (n != 0) {
if (count > 0) {
System.out.println();
}
sumX = 0;
sumY = 0;
Consumption[] list = new Consumption[n];
readEntries(br, list, n);
Arrays.sort(list);
Consumption[] list2 = new Consumption[n];
int length = checkAverage(list, list2, n);
count++;
printAnswer(list2, length, count);
n = reader(br);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Consumption implements Comparable<Consumption> {
public int x;
public int average;
public Consumption(int x, int average) {
this.x = x;
this.average = average;
}
public int compareTo(Consumption compareCons) {
int compare = compareCons.average;
return this.average-compare;
}
}
import java.util.*;
import java.text.*;
import java.math.*;
class Main {
public static int sumX;
public static int sumY;
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void readEntries(BufferedReader br, Consumption[] list, int n) throws NumberFormatException, IOException {
for (int i = 0; i < n; i++) {
int x = reader(br);
int y = reader(br);
sumX += x;
sumY += y;
Consumption c = new Consumption(x, y/x);
list[i] = c;
}
}
public static int checkAverage(Consumption[] list, Consumption[] list2, int n) {
int index = 0;
for (int i = 0; i < n; i++) {
boolean averageAdded = false;
if (list[i].x != -1) {
for (int j = i+1; j < n; j++) {
if (list[i].average != list[j].average) {
break;
}
else {
if (!averageAdded) {
Consumption c = new Consumption(list[i].x+list[j].x, list[i].average);
list2[index] = c;
averageAdded = true;
}
else {
Consumption c = new Consumption(list2[index].x+list[j].x, list2[index].average);
list2[index] = c;
}
Consumption tmp = new Consumption(-1, -1);
list[j] = tmp;
}
}
if (!averageAdded) { // if there was not any other slot equal do the current slot
Consumption c = new Consumption(list[i].x, list[i].average);
list2[index] = c;
}
index++;
}
}
return index;
}
public static void printAnswer(Consumption[] list2, int length, int count) {
System.out.println("Cidade# " + count + ":");
for (int i = 0; i < length-1; i++) {
System.out.print(list2[i].x + "-" + list2[i].average + " ");
}
System.out.println(list2[length-1].x + "-" + list2[length-1].average);
DecimalFormat df = new DecimalFormat("#.00");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("Consumo medio: " + df.format((double)sumY/sumX) + " m3.");
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = 0;
int n = reader(br);
while (n != 0) {
if (count > 0) {
System.out.println();
}
sumX = 0;
sumY = 0;
Consumption[] list = new Consumption[n];
readEntries(br, list, n);
Arrays.sort(list);
Consumption[] list2 = new Consumption[n];
int length = checkAverage(list, list2, n);
count++;
printAnswer(list2, length, count);
n = reader(br);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Consumption implements Comparable<Consumption> {
public int x;
public int average;
public Consumption(int x, int average) {
this.x = x;
this.average = average;
}
public int compareTo(Consumption compareCons) {
int compare = compareCons.average;
return this.average-compare;
}
}
Comments
Post a Comment