公式

D - Decrease 2 max elements 解説 by en_translator


This problem can be solved with a simulation. Rearranging \(A\) in descending order can be done with sort function in most programming languages. For more details, please refer to the following code.

Sample code (C++)

#include <bits/stdc++.h>

using namespace std;

int main() {
    // input
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &e : a) {
        cin >> e;
    }

    int answer = 0;
    while (true) {
        // sort
        sort(a.begin(), a.end(), greater());
        
        // check condition
        if (a[0] == 0 or a[1] == 0) {
            break;
        }
        
        // apply
        a[0] -= 1;
        a[1] -= 1;
        ++answer;
    }
    
    // output
    cout << answer << endl;
}

投稿日時:
最終更新: