Submission #632548

# Submission time Handle Problem Language Result Execution time Memory
632548 2022-08-20T10:22:28 Z MohammadAghil Liteh and Newfiteh (INOI20_litehfiteh) C++17
100 / 100
236 ms 135312 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pp;

#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff
#define ss

void debug(){
    cerr << endl;
}
template<typename Head, typename... Tail>
void debug(Head h, Tail... t){
    cerr << " " << h << ",";
    debug(t...);
}
#define er(...) cerr << __LINE__ << " (" << #__VA_ARGS__ << "):", debug(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void IOS(){
    cin.tie(0) -> sync_with_stdio(0);
//    #ifndef ONLINE_JUDGE
//        freopen("inp.txt", "r", stdin);
//        freopen("out.txt", "w", stdout);
//    #endif
}

const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 18, inf = ll(1e9) + 5;

int a[maxn];
int dp[maxn][lg][lg], rmq[maxn][lg], dp2[maxn];

int main(){ IOS();
    int n; cin >> n;
    rep(i,0,n){
        cin >> a[i];
        rmq[i][0] = a[i];
        rep(j,0,lg) rep(k,0,lg) dp[i][j][k] = inf;
    }


    rep(i,0,n){
        if(a[i] >= lg){
            return cout << -1 << '\n', 0;
        }
        dp[i][0][a[i]] = 0;
        if(a[i]) dp[i][0][a[i]-1] = 1;
    }

    rep(j,1,lg){
        rep(i,0,n-(1<<j)+1){
            rmq[i][j] = min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
        }
    }

    rep(j,1,lg){
        rep(i,0,n-(1<<j)+1){
            int mn = rmq[i][j];
            rep(k,0,mn+1){
                dp[i][j][k] = min({
                    dp[i][j-1][k] + dp[i+(1<<(j-1))][j-1][k],
                    (k < lg-1? dp[i][j-1][k+1] + dp[i+(1<<(j-1))][j-1][k+1] + 1: (int)inf),
                    (int)inf
                });
            }
//            er(i, j, dp[i][j][0]);
        }
    }


    rep(i,0,n){
        dp2[i] = inf;
        rep(j,0,lg){
            int l = i - (1<<j) + 1;
            if(l < 0) break;
            dp2[i] = min(dp2[i], dp[l][j][0] + (l?dp2[l-1]:0));
        }
    }

    cout << (dp2[n-1] == inf? -1: dp2[n-1]) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 1620 KB Output is correct
19 Correct 1 ms 1620 KB Output is correct
20 Correct 14 ms 13752 KB Output is correct
21 Correct 11 ms 13780 KB Output is correct
22 Correct 142 ms 134860 KB Output is correct
23 Correct 70 ms 134912 KB Output is correct
24 Correct 169 ms 135080 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 1620 KB Output is correct
29 Correct 12 ms 13716 KB Output is correct
30 Correct 12 ms 13800 KB Output is correct
31 Correct 11 ms 13708 KB Output is correct
32 Correct 12 ms 13780 KB Output is correct
33 Correct 132 ms 135096 KB Output is correct
34 Correct 236 ms 135240 KB Output is correct
35 Correct 182 ms 135220 KB Output is correct
36 Correct 146 ms 135244 KB Output is correct
37 Correct 220 ms 135156 KB Output is correct
38 Correct 155 ms 135144 KB Output is correct
39 Correct 142 ms 135036 KB Output is correct
40 Correct 179 ms 135188 KB Output is correct
41 Correct 146 ms 135208 KB Output is correct
42 Correct 194 ms 135312 KB Output is correct
43 Correct 139 ms 135144 KB Output is correct
44 Correct 151 ms 135116 KB Output is correct
45 Correct 143 ms 135148 KB Output is correct
46 Correct 147 ms 135116 KB Output is correct
47 Correct 112 ms 90228 KB Output is correct
48 Correct 103 ms 105128 KB Output is correct
49 Correct 126 ms 120140 KB Output is correct
50 Correct 145 ms 135112 KB Output is correct
51 Correct 11 ms 13780 KB Output is correct
52 Correct 139 ms 135024 KB Output is correct