Official

C - Final Day Editorial by en_translator


It is possible that the \(i\)-th student is ranked in the top \(K\) if and only if, should he/she get \(300\) points while the other students get \(0\) points on the \(4\)-th day, he/she would rank in the top \(K\).

Let \(S_i\) be the sum of \(i\)-th student’s score for the first three days. Also, let \(T\) be the \(K\)-th largest value among \(S_1, \dots, S_N\). If \(S_i + 300 \geq T\) then the answer is \(S_i + 300 \geq T\); otherwise the answer is No .

Sample code (C++):

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    k -= 1;
    vector<int> p(n);
    for (int& x : p) {
        int a, b, c;
        cin >> a >> b >> c;
        x = a + b + c;
    }
    vector<int> q = p;
    sort(begin(q), end(q), greater<>());
    for (int x : p) {
        cout << (x + 300 >= q[k] ? "Yes" : "No") << '\n';
    }
}

Sample code (Python) :

n, k = map(int, input().split())
s = [0] * n;
for i in range(n):
  s[i] = sum(map(int, input().split()))
t = sorted(s, reverse=True)[k - 1];
for x in s:
  print("Yes" if x + 300 >= t else "No")

In the codes above, the array is sorted in order to find the \(K\)-th largest value, so the worst complexity is \(\Theta (N \log N)\). However, one can use selection algorithm to reduce computational complexity to \(O(N)\).

In C++, std::nth_element is available:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    k -= 1;
    vector<int> p(n);
    for (int& x : p) {
        int a, b, c;
        cin >> a >> b >> c;
        x = a + b + c;
    }
    vector<int> q = p;
    nth_element(begin(q), begin(q) + k, end(q), greater<>());
    for (int x : p) {
        cout << (x + 300 >= q[k] ? "Yes" : "No") << '\n';
    }
}

posted:
last update: