QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#941489#10220. Picturesque SkylineSurfCoffee (Anton Stepanov, Alexey Vasilyev, Vladimir Novikov)TL 1ms3712kbC++232.3kb2025-03-18 15:32:592025-03-18 15:32:59

Judging History

This is the latest submission verdict.

  • [2025-03-18 15:32:59]
  • Judged
  • Verdict: TL
  • Time: 1ms
  • Memory: 3712kb
  • [2025-03-18 15:32:59]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (n); i += 1)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(a) ((int)(a).size())

mt19937 rnd(234);
const ll inf = 1e18;

const int maxn = 1e3 + 100;
int n;
int h[maxn];
int dp[maxn];
int cntl[maxn], cntr[maxn];
int s[2 * maxn];

int solve() {
    dp[0] = 0;
    for (int i = 1; i <= n; i += 1) {
        dp[i] = 10 * n + 100;
    }
    for (int i = 1; i + 1 < n; i += 1) {
        cntl[i] = cntr[i] = 0;
        for (int l = i - 1, r = i + 1; l >= 0 and r <= n - 1; l -= 1, r += 1) {
            cntl[l] = cntr[l] = cntl[r] = cntr[r] = 0;
            if (h[l] < h[r]) {
                cntr[l] += 1;
            } else {
                cntl[r] += 1;
            }
            for (int j = l + 1; j <= r - 1; j += 1) {
                cntr[l] += (h[j] > h[l]);
                cntl[j] += (h[l] > h[j]);
                cntl[r] += (h[j] > h[r]);
                cntr[j] += (h[r] > h[j]);
            }
            int opt = 0;
            for (int j = l; j <= r; j += 1) {
                opt += cntl[j];
            }
            int k = i - l;
            for (int j = 0; j <= 4 * k; ++j) {
                s[j] = 0;
            }
            for (int j = l; j <= r; j += 1) {
                if (cntr[j] != 0 or cntl[j] != 0) {
                    s[cntr[j] - cntl[j] + 2 * k] += 1;
                }
            }
            int num = k;
            for (int j = 0; j <= 4 * k && num > 0; j += 1) {
                while (num > 0 and s[j] > 0) {
                    --num;
                    --s[j];
                    opt += j - 2 * k;
                }
            }
            dp[r + 1] = min(dp[r + 1], dp[l] + opt);
        }
    }
    return dp[n];
}

int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    int t;
    cin >> t;
    rep(itr, t) {
        cin >> n;
        rep(i, n) {
            cin >> h[i];
        }
        int res = solve();
        cout << res << "\n";
    }
    return 0;
}

/*
1
6
1 2 4 8 16 32


4
3
1 3 2
6
1 2 4 8 16 32
5
1 2 3 4 5
7
6 4 2 1 3 5 7
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

4
3
1 3 2
6
1 2 4 8 16 32
5
1 2 3 4 5
7
6 4 2 1 3 5 7

output:

0
2
3
9

result:

ok 4 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
1000
1387006 1849130 3142445 4286557 8454496 12729744 12851090 12922927 13529136 13801515 16497484 18988046 22387865 22451673 22964870 24979445 27172325 35553226 38233807 40309279 43222378 45411991 45480206 51693604 51853974 57504350 64692967 67181740 70422962 73141342 76819930 76823620 77987836 ...

output:


result: