Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2479
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2479
Sorting Santa's List of Children
Por Cristian J. Ambrosi, URI Brazil
Timelimit: 1
Santa Claus is in finishing to organize the delivery of gifts to all the children of the world because Christmas is coming again. Looking at his new lists of children who will win gifts this year he realized that the trainee elf (who had been responsable for making the lists) did not put the names in alphabetical order.
As Santa is a very organized man, he wants that every list of children have, at its end, the total number of children who were well behaved this year and the total of those who were not. Then he can compare the number of children that was behaved in this year and in previous ones.
To help the good old man, your function is to create a program that reads all the names in the list and prints the same names in alphabetical order. At the bottom of the list, you should print the total number of children who were good behaved and the ones who were not.
Entrada
The input consists of several names. The first value N (0 ≤ N ≤ 100), indicates how many names there are on the list. The next N lines have one special character that matches with the behavior of the child (+ indicates that the child was well behaved, - indicates that the child misbehaved). After the special character, follows the name of the children with 20 characteres at maximum.
Saída
For each list of children, you should print the names in alphabetical order. After print the names, you must show the total number of children that were well behaved ("Se comportaram: ") and misbehaved during the year ("Nao se comportaram: ").
Input Example | Output Example |
16 |
Aninha Se comportaram: 7 | Nao se comportaram: 9 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
typedef struct{
char nome[21];
} nomes;
void ordena(nomes vet[], unsigned short);
void impressao(nomes vet[], unsigned short);
int main (void)
{
char comportamento;
unsigned short bemComportado = 0, mauComportado = 0;
unsigned short casos, aux, i;
scanf("%hu", &casos);
aux = casos;
nomes lista[aux];
i = 0;
while (casos--)
{
scanf(" %c %s", &comportamento, lista[i].nome);
i++;
if (comportamento == '+')
bemComportado++;
else
mauComportado++;
}
ordena(lista, aux);
impressao(lista, aux);
printf("Se comportaram: %hu | Nao se comportaram: %hu\n", bemComportado, mauComportado);
}
void ordena(nomes vet[], unsigned short tam)
{
short i = 1, j;
nomes pivo;
while (i < tam)
{
j = i - 1;
pivo = vet[i];
while (j >= 0 && strcmp(vet[j].nome, pivo.nome) > 0)
{
vet[j + 1] = vet[j];
j--;
}
vet[j + 1] = pivo;
i++;
}
}
void impressao(nomes vet[], unsigned short tam)
{
unsigned short i;
for (i = 0; i < tam; i++)
printf("%s\n", vet[i].nome);
}
Copy The Code &
Try With Live Editor
Input
+ Tininha
+ Dudinha
- Carlinhos
- Marquinhos
+ Joaozinho
+ Bruninha
- Leandrinho
- Fernandinha
+ Rafinha
- Pedrinho
+ Aninha
- Tamirinha
- Gaguinho
- Zezinho
- Luquinhas
+ Julhinha
Output
Bruninha
Carlinhos
Dudinha
Fernandinha
Gaguinho
Joaozinho
Julhinha
Leandrinho
Luquinhas
Marquinhos
Pedrinho
Rafinha
Tamirinha
Tininha
Zezinho
Se comportaram: 7 | Nao se comportaram: 9
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define endl '\n'
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, bem = 0, mal = 0;
cin >> n;
vector < string> nomes;
for (int i = 0; i < n; i++) {
string davez;
cin >> davez;
if (davez[0] == '+')
bem++;
else
mal++;
cin >> davez;
nomes.push_back(davez);
}
sort(nomes.begin(), nomes.end());
for (int i = 0; i < n; i++) {
cout << nomes[i] << endl;
}
cout << "Se comportaram: " << bem << " | Nao se comportaram: " << mal
<< endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
+ Tininha
+ Dudinha
- Carlinhos
- Marquinhos
+ Joaozinho
+ Bruninha
- Leandrinho
- Fernandinha
+ Rafinha
- Pedrinho
+ Aninha
- Tamirinha
- Gaguinho
- Zezinho
- Luquinhas
+ Julhinha
Output
Bruninha
Carlinhos
Dudinha
Fernandinha
Gaguinho
Joaozinho
Julhinha
Leandrinho
Luquinhas
Marquinhos
Pedrinho
Rafinha
Tamirinha
Tininha
Zezinho
Se comportaram: 7 | Nao se comportaram: 9
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [numTestCases, ...names] = readFileSync("/dev/stdin", "utf8").split("\n")
function main() {
const responses = []
const namesList = names.slice(0, +numTestCases)
const behaved = namesList.filter((name) => /^[+]/.test(name)).length
const notBehaved = namesList.filter((name) => /^[-]/.test(name)).length
const nameListOrdened = namesList
.map((name) => String(name.match(/\w+/g)))
.sort((a, b) => a.localeCompare(b))
responses.push(
nameListOrdened.join("\n"),
`Se comportaram: ${behaved} | Nao se comportaram: ${notBehaved}`
)
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
+ Tininha
+ Dudinha
- Carlinhos
- Marquinhos
+ Joaozinho
+ Bruninha
- Leandrinho
- Fernandinha
+ Rafinha
- Pedrinho
+ Aninha
- Tamirinha
- Gaguinho
- Zezinho
- Luquinhas
+ Julhinha
Output
Bruninha
Carlinhos
Dudinha
Fernandinha
Gaguinho
Joaozinho
Julhinha
Leandrinho
Luquinhas
Marquinhos
Pedrinho
Rafinha
Tamirinha
Tininha
Zezinho
Se comportaram: 7 | Nao se comportaram: 9
#4 Code Example with Python Programming
Code -
Python Programming
n = int(input())
r = b = 0
li = []
for i in range(n):
e = str(input()).split()
if e[0] == '+': b += 1
else: r += 1
li.append(e[1])
new = sorted(li)
for j in new:
print(j)
print('Se comportaram: {} | Nao se comportaram: {}'.format(b, r))
Copy The Code &
Try With Live Editor
Input
+ Tininha
+ Dudinha
- Carlinhos
- Marquinhos
+ Joaozinho
+ Bruninha
- Leandrinho
- Fernandinha
+ Rafinha
- Pedrinho
+ Aninha
- Tamirinha
- Gaguinho
- Zezinho
- Luquinhas
+ Julhinha
Output
Bruninha
Carlinhos
Dudinha
Fernandinha
Gaguinho
Joaozinho
Julhinha
Leandrinho
Luquinhas
Marquinhos
Pedrinho
Rafinha
Tamirinha
Tininha
Zezinho
Se comportaram: 7 | Nao se comportaram: 9