結果

問題 No.2036 Max Middle
ユーザー Ekiben542Ekiben542
提出日時 2024-05-13 11:36:47
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,107 bytes
コンパイル時間 1,428 ms
コンパイル使用メモリ 97,704 KB
実行使用メモリ 13,764 KB
最終ジャッジ日時 2024-05-13 11:36:54
合計ジャッジ時間 5,642 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,764 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>

using namespace std;

bool is_peak(int i, const vector<int>& A) {
    int N = A.size();
    return 0 < i && i < N - 1 && A[i - 1] < A[i] && A[i] > A[i + 1];
}

int count_operations(int N, vector<int>& A) {
    set<int> peaks;
    for (int i = 1; i < N - 1; ++i) {
        if (is_peak(i, A)) {
            peaks.insert(i);
        }
    }

    int count = 0;
    while (!peaks.empty()) {
        vector<int> to_check(peaks.begin(), peaks.end());
        for (int i : to_check) {
            if (peaks.count(i)) {
                peaks.erase(i);
                count += 1;
                A[i] = min(A[i - 1], A[i + 1]) - 1;
                for (int j : {i - 1, i + 1}) {
                    if (is_peak(j, A)) {
                        peaks.insert(j);
                    }
                }
            }
        }
    }

    return count;
}

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    int result = count_operations(N, A);
    cout << result << endl;

    return 0;
}
0