Submission #782716

# Submission time Handle Problem Language Result Execution time Memory
782716 2023-07-14T08:18:29 Z RushB Liteh and Newfiteh (INOI20_litehfiteh) C++17
0 / 100
76 ms 1748 KB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++) 
#define int long long
const int64_t INF = 1 << 30;
const int N = 1e5 + 5;
const int LG = 17;
int dp[N][LG][LG], mn[N][LG], a[N], n, lg, cp[N];

/*
int solve(int x) {
        int tx = x, cur = 0, ans = 0;
        FOR(i, 0, LG) if (tx >> i & 1) {
                ans += dp[cur][i][0];
                cur += 1 << i;
        }
        tx = n - x, cur = n;
        FOR(i, 0, LG) if (tx >> i & 1) {
                cur -= 1 << i;
                ans += dp[cur][i][0];
        }
        return ans;
}
*/

void add(int l, int r) {
        while (l < r) cp[l++]--;
}
int solve(int x) {
        copy(a, a + n, cp);
        int ans = 0;
        for (int i = LG - 1; i >= 0; i--) {
                int tx = x;
                while (tx < n) {
                        int m = *min_element(cp + tx, cp + tx + (1 << i)); 
                        if (m >= 1) add(tx, tx + (1 << i)), ans++;
                        tx += 1 << i;
                }
                if (x >> i & 1) x ^= 1 << i;
        }
        int m = *max_element(cp, cp + n);
        return (m == 0 ? ans : INF);
}

signed main() {
        ios::sync_with_stdio(0), cin.tie(0);
        cin >> n;
        FOR(i, 0, n) cin >> a[i];
        
        for (int x = n - 1; x >= 0; x--) {
                mn[x][0] = a[x];
                for (int i = 1; (1 << i) + x <= n; i++)
                        mn[x][i] = min(mn[x][i - 1], mn[x + (1 << (i - 1))][i - 1]);
                FOR(j, 0, LG) {
                        if (a[x] <= j + 1) dp[x][0][j] = (a[x] == j + 1);
                        else dp[x][0][j] = INF;
                }
                for (int i = 1; (1 << i) + x <= n; i++) {
                        FOR(j, 0, LG) {
                                dp[x][i][j] = dp[x][i - 1][j] + dp[x + (1 << (i - 1))][i - 1][j];
                                if (mn[x][i] > j) 
                                        dp[x][i][j] = min(dp[x][i][j], dp[x][i - 1][j + 1] + dp[x + (1 << (i - 1))][i - 1][j + 1] + 1);
                        }
                }
        }
        int ans = INF;
        FOR(i, 0, n) {
                ans = min(ans, solve(i));
        }
        if (ans >= INF) ans = -1;
        cout << ans << '\n';
}
//10:39:29
# Verdict Execution time Memory Grader output
1 Incorrect 76 ms 1748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 76 ms 1748 KB Output isn't correct
2 Halted 0 ms 0 KB -