Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2381
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2381
Call List
By OBI - Brazilian Informatics Olympiad 2010 Brazil
Timelimit: 1
Joana is a respected teacher and has several students. In her last class, she promised that she would raffle off a student to earn a special bonus on the final grade: she put N pieces of paper numbered from 1 to N in a bag and drew a certain number K; the winning student was the Kth student on the roll call.
The problem is that Joana forgot her class diary, so she has no way of knowing which number corresponds to which student. She knows the names of all the students, and that their numbers, from 1 to N, are assigned in alphabetical order, but her students are very anxious and want to know right away who was the winner.
Given the names of Joana's students and the number drawn, determine the name of the student who should receive the bonus.
Input
The first line contains two integers N and K separated by a blank space (1 ≤ K ≤ N ≤ 100). Each of the following N lines contains a string of minimum length 1 and maximum length 20 representing the names of the students. Names are made up of all lowercase letters from 'a' to 'z'.
Output
Your program must print a single line, containing the name of the student who is to receive the bonus.
Sample Input | Sample Output |
5 1 maria joao carlos vanessa jose |
carlos |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
typedef struct{
char nome[30];
} string;
void ordena(string *, unsigned short tam);
int main (void)
{
unsigned short i;
unsigned short qtsAlunos, alunoPremiado;
scanf("%hu %hu", &qtsAlunos, &alunoPremiado);
string alunos[qtsAlunos];
for (i = 0; i < qtsAlunos; i++)
scanf("%s", alunos[i].nome);
ordena(alunos, qtsAlunos);
printf("%s\n", alunos[alunoPremiado - 1].nome);
}
void ordena(string *alunos, unsigned short tam)
{
short i = 1, j;
string pivo;
while (i < tam)
{
j = i - 1;
pivo = alunos[i];
while (j >= 0 && strcmp(alunos[j].nome, pivo.nome) > 0)
{
alunos[j + 1] = alunos[j];
j--;
}
alunos[j + 1] = pivo;
i++;
}
}
Copy The Code &
Try With Live Editor
Input
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int a, b, i;
cin >> a >> b;
vector < string> nomes(a);
for (i = 0; i < a; i++) {
cin >> nomes[i];
}
sort(nomes.begin(), nomes.end());
cout << nomes[b - 1] << endl;
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Javascript Programming
Code -
Javascript Programming
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int a, b, i;
cin >> a >> b;
vector < string> nomes(a);
for (i = 0; i < a; i++) {
cin >> nomes[i];
}
sort(nomes.begin(), nomes.end());
cout << nomes[b - 1] << endl;
return 0;
}
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
Python Programming
n, k = [int(x) for x in input().split()]
nm = []
for g in range(n): nm.append(input())
print(sorted(nm)[k-1])
Copy The Code &
Try With Live Editor