Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2312
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2312
Medal Table
By Leonardo Fernandes, IFSC Brazil
Timelimit: 1
Somebody messed up the olympic medal table. Your program must put it back in the right order. The order of the countries in the medal table is given by the number of gold medals. If there's a tie in the number of gold medals, than the country with more silver medals win. If there's a tie in both gold and silver medals, the country with more bronze medals should be above. If two or more nations tie in the three kinds of medals, your program must show them in alphabetic order.
Input
Input is the number of participating nations N (0 ≤ N ≤ 500), followed by the list of countries and their gold G (0 ≤ G ≤ 10000), silver S (0 ≤ S ≤ 10000) and bronze B (0 ≤ B ≤ 10000) medals.
Output
Output should be the list of countries and their gold, silver and bronze medals shown in the specified order.
Input Sample | Output Sample |
8 |
Franca 10 18 14 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
typedef struct{
char pais[20];
unsigned short ouro, prata, bronze;
} medalhas;
void preencheQuadro(medalhas vet[], unsigned short tam);
void ordena(medalhas vet[], unsigned short tam);
void ordenaAlpha(medalhas vet[], unsigned short tam);
void ordenaPrata(medalhas vet[], unsigned short tam);
void ordenaBronze(medalhas vet[], unsigned short tam);
void structPress(medalhas vet[], unsigned short tam);
int main (void)
{
unsigned short casos;
scanf("%hu", &casos);
// Vetor de Structs do tipo Medalha;
medalhas paisMedalhas[casos + 1];
preencheQuadro(paisMedalhas, casos);
ordenaAlpha(paisMedalhas, casos);
ordenaBronze(paisMedalhas, casos);
ordenaPrata(paisMedalhas, casos);
ordena(paisMedalhas, casos);
structPress(paisMedalhas, casos);
}
void preencheQuadro(medalhas vet[], unsigned short tam)
{
unsigned short i;
for (i = 0; i < tam; i++)
scanf(" %s %hu %hu %hu", vet[i].pais, &vet[i].ouro, &vet[i].prata, &vet[i].bronze);
}
void ordena(medalhas vet[], unsigned short tam)
{
short i = 1, j;
medalhas pivo;
while (i < tam)
{
j = i - 1;
pivo = vet[i];
while (j >= 0 && vet[j].ouro < pivo.ouro)
{
vet[j + 1] = vet[j];
j--;
}
vet[j + 1] = pivo;
i++;
}
}
void structPress(medalhas vet[], unsigned short tam)
{
unsigned short i;
for (i = 0; i < tam; i++)
printf("%s %hu %hu %hu\n", vet[i].pais, vet[i].ouro, vet[i].prata, vet[i].bronze);
}
void ordenaAlpha(medalhas vet[], unsigned short tam)
{
short i = 1, j;
medalhas pivo;
while (i < tam)
{
j = i - 1;
pivo = vet[i];
while (j >= 0 && strcmp(vet[j].pais, pivo.pais) > 0)
{
vet[j + 1] = vet[j];
j--;
}
vet[j + 1] = pivo;
i++;
}
}
void ordenaBronze(medalhas vet[], unsigned short tam)
{
short i = 1, j;
medalhas pivo;
while (i < tam)
{
j = i - 1;
pivo = vet[i];
while (j >= 0 && vet[j].bronze < pivo.bronze)
{
vet[j + 1] = vet[j];
j--;
}
vet[j + 1] = pivo;
i++;
}
}
void ordenaPrata(medalhas vet[], unsigned short tam)
{
short i = 1, j;
medalhas pivo;
while (i < tam)
{
j = i - 1;
pivo = vet[i];
while (j >= 0 && vet[j].prata < pivo.prata)
{
vet[j + 1] = vet[j];
j--;
}
vet[j + 1] = pivo;
i++;
}
}
Copy The Code &
Try With Live Editor
Input
Belgica 2 2 2
Brasil 7 6 6
Franca 10 18 14
Italia 8 12 8
Australia 8 11 10
Colombia 3 2 3
Suica 3 2 2
Tailandia 2 2 2
Output
Italia 8 12 8
Australia 8 11 10
Brasil 7 6 6
Colombia 3 2 3
Suica 3 2 2
Belgica 2 2 2
Tailandia 2 2 2
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
void bubble_sort(int a[],int b[],int c[],string d[],int n);
int main()
{
int n;
cin >> n;
int a[n],b[n],c[n];
string d[n];
for(int i = 0; i < n; i++)
cin >> d[i] >> a[i] >> b[i] >> c[i];
bubble_sort(a,b,c,d,n);
for(int i = 0; i < n; i++)
cout << d[i] << " " << a[i] << " " << b[i] << " " << c[i] << endl;
return 0;
}
void bubble_sort(int a[],int b[],int c[],string d[],int n)
{
int i=0;
while(i < n-1)
{
int k=0;
while(k < n-i-1)
{
if(a[k+1] > a[k])
{
swap(a[k],a[k+1]);
swap(b[k],b[k+1]);
swap(c[k],c[k+1]);
swap(d[k],d[k+1]);
}
if(a[k+1]==a[k])
{
if(b[k+1] > b[k])
{
swap(a[k],a[k+1]);
swap(b[k],b[k+1]);
swap(c[k],c[k+1]);
swap(d[k],d[k+1]);
}
if(b[k+1] == b[k])
{
if(c[k+1] > c[k])
{
swap(a[k],a[k+1]);
swap(b[k],b[k+1]);
swap(c[k],c[k+1]);
swap(d[k],d[k+1]);
}
if(c[k]==c[k+1])
{
if(d[k+1] < d[k])
{
swap(a[k],a[k+1]);
swap(b[k],b[k+1]);
swap(c[k],c[k+1]);
swap(d[k],d[k+1]>;
}
}
}
}
k++;
}
i++;
}
}
Copy The Code &
Try With Live Editor
Input
Belgica 2 2 2
Brasil 7 6 6
Franca 10 18 14
Italia 8 12 8
Australia 8 11 10
Colombia 3 2 3
Suica 3 2 2
Tailandia 2 2 2
Output
Italia 8 12 8
Australia 8 11 10
Brasil 7 6 6
Colombia 3 2 3
Suica 3 2 2
Belgica 2 2 2
Tailandia 2 2 2
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main {
static Reader in = new Reader(System.in);
static Writer out = new Writer(System.out);
public static void main(String[] args) throws IOException {
int N = in.nextInt();
Country[] medals = new Country[N];
for (int i = 0; i < N; i++) {
medals[i] = new Country(in.next(), in.nextInt(), in.nextInt(), in.nextInt());
}
Country m;
Arrays.sort(medals, new Comparator < Country>() {
@Override
public int compare(Country m1, Country m2) {
if (m1.gM != m2.gM) {
return m2.gM - m1.gM;
} else if (m2.sM != m1.sM) {
return m2.sM - m1.sM;
} else if (m2.bM != m1.bM) {
return m2.bM - m1.bM;
} else {
return m1.name.compareTo(m2.name);
}
}
});
for (int i = 0; i < N; i++) {
m = medals[i];
out.println(m.name, m.gM, m.sM, m.bM);
}
in.close();
out.flush();
out.close();
}
static class Country {
public String name;
public int gM;
public int sM;
public int bM;
public Country(String name, int gM, int sM, int bM) {
this.name = name;
this.gM = gM;
this.sM = sM;
this.bM = bM;
}
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////// INPUT / OUTPUT /////////////////////////////
////////////////////////////////////////////////////////////////////////////
static class Reader implements Closeable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public Reader(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
private StringTokenizer getTokenizer() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer;
}
public boolean hasNext() throws IOException {
return getTokenizer() != null;
}
public String next() throws IOException {
return hasNext() ? tokenizer.nextToken() : null;
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String[] nextStringArray(int size) throws IOException {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = next();
}
return array;
}
public int[] nextIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) throws IOException {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
@Override
public void close() throws IOException {
tokenizer = null;
reader.close();
}
}
static class Writer implements Closeable, Flushable {
private final PrintWriter writer;
public Writer(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
@Override
public void close() {
writer.close();
}
@Override
public void flush() {
writer.flush();
}
}
}
Copy The Code &
Try With Live Editor
Input
Belgica 2 2 2
Brasil 7 6 6
Franca 10 18 14
Italia 8 12 8
Australia 8 11 10
Colombia 3 2 3
Suica 3 2 2
Tailandia 2 2 2
Output
Italia 8 12 8
Australia 8 11 10
Brasil 7 6 6
Colombia 3 2 3
Suica 3 2 2
Belgica 2 2 2
Tailandia 2 2 2
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [[numLines], ...lines] = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map((line) => line.split(" "))
/** @typedef {[string, number, number, number]} olympicNationType */
/**
* @param {olympicNationType} nationA
* @param {olympicNationType} nationB
*/
function olimpicsMedalsCompare(nationA, nationB) {
if (nationA[1] !== nationB[1]) return nationB[1] - nationA[1] // DESC - Medals O
else if (nationA[2] !== nationB[2]) return nationB[2] - nationA[2] // DESC - Medals P
else if (nationA[3] !== nationB[3]) return nationB[3] - nationA[3] // DESC - Medals B
else if (nationA[0] !== nationB[0]) return nationA[0].localeCompare(nationB[0]) // ASC - name
else return 0
}
function main() {
const size = Number.parseInt(numLines, 10)
const responses = lines
.slice(0, size)
.sort(olimpicsMedalsCompare)
.map(line => line.join(" "))
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Belgica 2 2 2
Brasil 7 6 6
Franca 10 18 14
Italia 8 12 8
Australia 8 11 10
Colombia 3 2 3
Suica 3 2 2
Tailandia 2 2 2
Output
Italia 8 12 8
Australia 8 11 10
Brasil 7 6 6
Colombia 3 2 3
Suica 3 2 2
Belgica 2 2 2
Tailandia 2 2 2
#5 Code Example with Python Programming
Code -
Python Programming
plac = []
for g in range(int(input())):
tmp = []
e = input().split()
tmp.append(e[0])
tmp.append(int(e[1]))
tmp.append(int(e[2]))
tmp.append(int(e[3]))
plac.append(tmp)
plac = sorted(sorted(plac, key=lambda x: x[0]), key=lambda x: (x[1], x[2], x[3]), reverse=True)
for n, o, p, b in plac: print(n, o, p, b)
Copy The Code &
Try With Live Editor
Input
Belgica 2 2 2
Brasil 7 6 6
Franca 10 18 14
Italia 8 12 8
Australia 8 11 10
Colombia 3 2 3
Suica 3 2 2
Tailandia 2 2 2
Output
Italia 8 12 8
Australia 8 11 10
Brasil 7 6 6
Colombia 3 2 3
Suica 3 2 2
Belgica 2 2 2
Tailandia 2 2 2