QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125799#6626. Real Mountainsbashkort0 1ms3516kbC++202.4kb2023-07-17 17:46:112023-07-17 17:46:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 17:46:14]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3516kb
  • [2023-07-17 17:46:11]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MOD = 1e6 + 3;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<int> h(n);

    for (int i = 0; i < n; ++i) {
        cin >> h[i];
    }

    ll best = 3e18;
    int p = max_element(h.begin(), h.end()) - h.begin();

    vector<int> need(h);

    for (int i = 1; i <= p; ++i) {
        need[i] = max(need[i], need[i - 1]);
    }
    for (int i = n - 2; i >= p; --i) {
        need[i] = max(need[i], need[i + 1]);
    }

    vector<set<int>> pos(h[p] + 1);
    vector<set<int>> alive(h[p] + 1);

    for (int i = 0; i < n; ++i) {
        pos[h[i]].insert(i);
        if (h[i] < need[i]) {
            alive[h[i]].insert(i);
        }
    }

    auto bestL = [&](int i) -> int {
        for (int k = h[i] + 1; k <= h[p]; ++k) {
            if (!pos[k].empty() && *pos[k].begin() < i) {
                return k;
            }
        }
        return 1e9;
    };

    auto bestR = [&](int i) -> int {
        for (int k = h[i] + 1; k <= h[p]; ++k) {
            if (!pos[k].empty() && *pos[k].rbegin() > i) {
                return k;
            }
        }
        return 1e9;
    };

    ll ans = 0;

    auto inc = [&](int a) {
        pos[h[a]].erase(a);
        alive[h[a]].erase(a);
        h[a] += 1;
        pos[h[a]].insert(a);
        if (h[a] < need[a]) {
            alive[h[a]].insert(a);
        }
    };

    while (true) {
        int a = -1, b = -1;
        for (int k = 0; k < size(alive); ++k) {
            if (!alive[k].empty()) {
                a = *alive[k].begin();
                b = *alive[k].rbegin();
                break;
            }
        }
        if (a == -1) {
            break;
        }
        if (a != b) {
            ll LA = bestL(a), RB = bestR(b);
            ll A = LA + bestR(a), B = bestL(b) + RB;
            ll costA = A + RB + h[a] + 1;
            ll costB = B + LA + h[b] + 1;
            if (costA > costB) {
                swap(a, b);
                swap(A, B);
                swap(LA, RB);
            }
            ans += A + h[a];
            ans += RB + h[a] + h[b];
            inc(a), inc(b);
        } else {
            ans += bestL(a) + bestR(a) + h[a];
            inc(a);
        }
    }

    best = min(best, ans);

    cout << best % MOD << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 1ms
memory: 3428kb

input:

3
29 9 9

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3516kb

input:

3
62 20 71

output:

7287

result:

ok 1 number(s): "7287"

Test #3:

score: -3
Wrong Answer
time: 1ms
memory: 3420kb

input:

10
72 33 22 22 13 49 53 57 72 85

output:

40295

result:

wrong answer 1st numbers differ - expected: '40403', found: '40295'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #2:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

0%