QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125793 | #6626. Real Mountains | bashkort | 0 | 1ms | 3476kb | C++20 | 1.8kb | 2023-07-17 17:31:09 | 2023-07-17 17:31:11 |
Judging History
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]);
}
auto cost = [&](int i) -> int {
if (need[i] == h[i]) {
return -1e9;
}
int l = 1e9, r = 1e9;
for (int j = 0; j < i; ++j) {
if (h[j] > h[i]) {
l = min(l, h[j]);
}
}
for (int j = i + 1; j < n; ++j) {
if (h[j] > h[i]) {
r = min(r, h[j]);
}
}
return l + r;
};
ll ans = 0;
while (h != need) {
int minH = numeric_limits<int>::max();
for (int i = 0; i < n; ++i) {
if (h[i] < need[i]) {
minH = min(minH, h[i]);
}
}
int a = -1, b = -1;
for (int i = 0; i < n; ++i) {
if (h[i] < need[i] && h[i] == minH) {
b = i, a = (a == -1 ? i : a);
}
}
ll A = cost(a);
ll B = cost(b);
h[a] += 1;
ll costA = A + cost(b);
h[a] -= 1;
h[b] += 1;
ll costB = B + cost(a);
h[b] -= 1;
if (costA > costB) {
swap(a, b);
swap(A, B);
}
ans += A + h[a];
h[a] += 1;
}
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
Time Limit Exceeded
Test #1:
score: 3
Accepted
time: 0ms
memory: 3408kb
input:
3 29 9 9
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
3 62 20 71
output:
7287
result:
ok 1 number(s): "7287"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3476kb
input:
10 72 33 22 22 13 49 53 57 72 85
output:
40403
result:
ok 1 number(s): "40403"
Test #4:
score: -3
Time Limit Exceeded
input:
5000 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 97 97 97 97 9...
output:
result:
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%