답안 #400887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
400887 2021-05-08T19:17:22 Z DavidDamian Pod starim krovovima (COCI20_psk) C++11
50 / 50
2 ms 204 KB
#include <iostream>
#include <algorithm>
///Greedy
///Custom sorting
///Drop water to glasses with maximum volume
using namespace std;
typedef long long ll;
struct vaso
{
    int vol;
    int agua;
    int id;
};

bool porVolumen(vaso A, vaso B)
{
    if (A.vol < B.vol) return true;
    else {
        if (A.vol == B.vol) {
            if (A.agua < B.agua) return true;
            else return false;
        }
        else return false;
    }
}

bool porId(vaso A, vaso B)
{
    return A.id < B.id;
}

vaso A[100005];

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> A[i].agua >> A[i].vol;
        A[i].id = i;
    }
    sort(A + 1, A + 1 + n, porVolumen);
    int j_max = n;
    while (j_max >= 1 && A[j_max].vol - A[j_max].agua == 0){
        j_max--;
    }
    for (int i = 1; i <= n; i++) {
        if (i >= j_max) {
            break;
        }
        while (A[i].agua > 0){
            int agua = min(A[j_max].vol - A[j_max].agua, A[i].agua);
            A[j_max].agua += agua;
            A[i].agua -= agua;
            if (A[j_max].vol - A[j_max].agua == 0) {
                j_max--;
            }
            if (i >= j_max) {
                break;
            }
        }
    }
    sort(A + 1, A + 1 + n, porId);
    int total = 0;
    for (int i = 1; i <= n; i++) {
        if (A[i].agua == 0) {
            total++;
        }
    }
    cout << total << endl;
    for (int i = 1; i <= n; i++) {
        cout << A[i].agua << " ";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 2 ms 204 KB Output is correct