Submission #238224

# Submission time Handle Problem Language Result Execution time Memory
238224 2020-06-10T09:31:09 Z NONAME Krov (COCI17_krov) C++17
70 / 140
1500 ms 768 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define el '\n'
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
using namespace std;

typedef long long ll;

const int N = 1e5;

int n, h[N];
ll ans = 0;

ll f(ll x, int ps) {
    ll res = 0;

    for (int i = 0; i < n; ++i)
        res += abs(h[i] - abs(x - abs(ps - i)));

    return res;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

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

    ans = -1;

    for (int i = 0; i < n; ++i) {
        ll l = max(i + 1, n - i), r = 2e9;

        while (l < r) {
            ll ri = (l + r) >> 1;
            ll le = ri + 1;

            ll fri = f(ri, i);
            ll fle = f(le, i);

            if (fri == fle) l = r = ri;
            if (fri < fle) r = ri;
            if (fle < fri) l = le;
        }

        ll cur = f(l, i);

        ans = (ans == -1 ? cur : min(ans, cur));
    }

    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 120 ms 384 KB Output is correct
2 Correct 149 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 384 KB Output is correct
2 Correct 148 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 384 KB Output is correct
2 Correct 346 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 600 ms 384 KB Output is correct
2 Correct 650 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1152 ms 504 KB Output is correct
2 Correct 816 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1591 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1589 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1598 ms 640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1585 ms 768 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1576 ms 768 KB Time limit exceeded
2 Halted 0 ms 0 KB -