(UVA) Sales - Solution
Brute Force was used to solve this problem.
import java.io.*;
import java.util.*;
class Main {
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 process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numTests = reader(br);
for (int i = 0; i < numTests; i++) {
int listSize = reader(br);
int[] list = new int[listSize];
for (int j = 0; j < listSize; j++) {
list[j] = reader(br);
}
int totalWorstSales = 0;
for (int j = 1; j < listSize; j++) {
int worstSales = 0;
for (int k = 0; k < j; k++) {
if (list[k] <= list[j]) {
worstSales++;
}
}
totalWorstSales += worstSales;
}
System.out.println(totalWorstSales);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
class Main {
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 process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numTests = reader(br);
for (int i = 0; i < numTests; i++) {
int listSize = reader(br);
int[] list = new int[listSize];
for (int j = 0; j < listSize; j++) {
list[j] = reader(br);
}
int totalWorstSales = 0;
for (int j = 1; j < listSize; j++) {
int worstSales = 0;
for (int k = 0; k < j; k++) {
if (list[k] <= list[j]) {
worstSales++;
}
}
totalWorstSales += worstSales;
}
System.out.println(totalWorstSales);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment