본문 바로가기

Competition/codeforces

Codeforces Round #525 (Div. 2) B. Ehab and subtraction

B. Ehab and subtraction

https://codeforces.com/problemset/problem/1088/B


sort를 사용하면 쉬운 문제이다.

받은 수를 정렬 후, 앞에서부터 차례대로 확인한다.

이때 최소치보다 큰 경우 arr[i] - minv 값을 출력하고 minv 값을 갱신한다.

그리고 한번 출력될 때 trr값을 1씩 더해준다.

모든 수를 다 돌아보고 난 후, k번 출력을 채워주기 위해, k - trr번 0을 출력한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
 
int n, k, arr[100001];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> k;
    for (int i = 0; i < n; i++cin >> arr[i];
    sort(arr, arr + n);
    int minv = 0;
    int trr = 0;
    for (int i = 0; i < n; i++) {
        if (trr == k) break;
        if (arr[i] > minv) {
            ++trr;
            cout << arr[i] - minv << '\n';
            minv += arr[i] - minv;
        }
    }
    for (int j = trr; j < k; j++) {
        cout << "0\n";
    }
    return 0;
}
cs