Algorithm
Problem Name: beecrowd | 2534
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2534
General Exam
By Flávio Zavan, UFPR Brazil
Timelimit: 1
The national math exam is done every leap year in Nlogonia. Every citizen is evaluated, so it is possible to study the development of logic and math in the country along the years.
After their exams are graded, the citizens are sorted according to their grades (the higher the grade, the better the citizen). They get discounts in their taxes according to their positions in this rank.
The Statistic Central Office (SCO) is in charge of processing the grades obtained in the exam. This year, however, Vasya, one of the people in charge, is at the hospital, and so you were hired to finish his job.
Write a program that, given the number of citizens and their grades, answers queries informing the grade of the citizen that is ranked at a given position.
Input
The input contains several test cases. The first line of each test case contains two integers N (1 ≤ N ≤ 100) and Q (1 ≤ Q ≤ 100), the number of citizens and the number of queries.
Each of the next N lines contains the grade ni obtained by the i-th citizen (0 ≤ ni ≤ 30000).
Each of the next Q lines contains a position pi.
The input ends with end-of-file (EOF).
Output
For each test case, print, for each query, a line containing the grade of the citizen that is ranked at position pi.
Input Sample | Output Sample |
6 5 |
250 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main(void)
{
int i, j, x, y, a, b, temp;
while (scanf("%i %i", &a, &b) != EOF)
{
int a1[a], a2[b];
for (i = 0; i < a; ++i)
scanf("%i", &a1[i]);
for (i = 0; i < a-1; ++i)
{
for (j = i+1; j < a; ++j)
{
if (a1[j] > a1[i])
{
temp = a1[i];
a1[i] = a1[j];
a1[j] = temp;
}
}
}
for (i = 0; i < b; ++i)
scanf("%i", &a2[i]);
for (i = 0; i < b; ++i)
printf("%i\n", a1[a2[i]-1]);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
int main(){
int n[100];
int q;
int N,Q;
while(cin >> N >> Q)
{
for(int i = 0; i < N; i++)
cin >> n[i];
sort(n,n+N,greater < int>());
for(int i = 0; i < Q; i++)
{
cin >> q;
cout << n[q-1] << endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8")
.split(/\s+/)
.map(value => Number.parseInt(value, 10))
function main() {
const output = []
while (input.length > 0) {
const N = input.shift()
const Q = input.shift()
const grades = input.splice(0, N).sort((a, b) => b - a)
const queries = input.splice(0, Q).map((query) => grades[query - 1])
Reflect.apply(Array.prototype.push, output, queries)
}
console.log(output.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
while True:
try:
n, q = [int(x) for x in input().split()]
cid = []
while n:
n -= 1
cid.append(int(input()))
cid.sort(reverse=True)
while q:
q -= 1
p = input()
print(cid[int(p) - 1])
except EOFError:
break
Copy The Code &
Try With Live Editor
Input
Output