Submission #469078

# Submission time Handle Problem Language Result Execution time Memory
469078 2021-08-30T16:04:11 Z Millad Liteh and Newfiteh (INOI20_litehfiteh) C++14
100 / 100
355 ms 347388 KB
/// In the name of god
#include <bits/stdc++.h>
     
#define F first
#define S second
#define pb push_back
#define debug(x) cerr << #x << " : " << x << '\n'
     
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
     
const ll maxn = 1e5 + 5;
const ll inf = 1e18;
const ll lg = 20;
ll n, a[maxn], dp[maxn][lg + 1][lg + 1], pw2[lg + 1], ANS[maxn];
int main(){
	pw2[0] = 1;
	for(ll i = 1; i < lg; i ++)pw2[i] = (pw2[i - 1] * 2ll);
    	cin >> n;
    	for(ll i = 0; i < n; i ++){
    		cin >> a[i]; ANS[i] = inf;
    		for(ll j = 0; j < lg; j ++){
    			if(a[i] < j)dp[i][0][j] = -1;
    			else if(a[i] == j)dp[i][0][j] = 0;
    			else if(a[i] == j + 1)dp[i][0][j] = 1;
    			else dp[i][0][j] = -1;
    		}
    		if(dp[i][0][0] != -1){
				if(!i)ANS[i] = min(ANS[i], dp[i][0][0]);
				else if(ANS[i - 1] != -1)ANS[i] = min(ANS[i], dp[i][0][0] + ANS[i - 1]); 
			}
    		ll d;
    		for(ll j = 1; j < lg; j ++){
    			for(ll k = 0; k < lg; k ++){
    				if(pw2[j] > (i + 1ll)){
					dp[i][j][k] = -1;
					continue;
				}
				d = inf;
				if((k < (lg - 1ll)) &&(dp[i][j - 1][k + 1] != -1) && (dp[i - pw2[j - 1]][j - 1][k + 1] != -1))
                                        d = min(d, dp[i - pw2[j - 1]][j - 1][k + 1] + dp[i][j - 1][k + 1] + 1ll);
				if((dp[i][j - 1][k] != -1) && (dp[i - pw2[j - 1]][j - 1][k] != -1))
                                        d = min(d, dp[i - pw2[j - 1]][j - 1][k] + dp[i][j - 1][k]);
				if(d == inf)d = -1;
				dp[i][j][k] = d;
    			}
    			if(dp[i][j][0] != -1){
    				if(pw2[j] == i + 1ll)ANS[i] = min(ANS[i], dp[i][j][0]);
    				else if(ANS[i - pw2[j]] != -1)ANS[i] = min(ANS[i], dp[i][j][0] + ANS[i - pw2[j]]); 
    			}
    		}
    		if(ANS[i] == inf)ANS[i] = -1;
    	}
    	cout << ANS[n - 1];
    }
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 2 ms 2380 KB Output is correct
5 Correct 2 ms 2380 KB Output is correct
6 Correct 2 ms 2380 KB Output is correct
7 Correct 2 ms 2380 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 2 ms 2380 KB Output is correct
10 Correct 2 ms 2380 KB Output is correct
11 Correct 2 ms 2380 KB Output is correct
12 Correct 2 ms 2380 KB Output is correct
13 Correct 2 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 2 ms 2380 KB Output is correct
5 Correct 2 ms 2380 KB Output is correct
6 Correct 2 ms 2380 KB Output is correct
7 Correct 2 ms 2380 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 2 ms 2380 KB Output is correct
10 Correct 2 ms 2380 KB Output is correct
11 Correct 2 ms 2380 KB Output is correct
12 Correct 2 ms 2380 KB Output is correct
13 Correct 2 ms 2380 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 3 ms 3660 KB Output is correct
19 Correct 3 ms 3660 KB Output is correct
20 Correct 30 ms 34984 KB Output is correct
21 Correct 28 ms 34912 KB Output is correct
22 Correct 291 ms 347216 KB Output is correct
23 Correct 304 ms 347336 KB Output is correct
24 Correct 294 ms 347236 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 3 ms 3788 KB Output is correct
29 Correct 29 ms 34892 KB Output is correct
30 Correct 29 ms 34980 KB Output is correct
31 Correct 32 ms 34964 KB Output is correct
32 Correct 30 ms 34892 KB Output is correct
33 Correct 340 ms 347172 KB Output is correct
34 Correct 347 ms 347240 KB Output is correct
35 Correct 338 ms 347268 KB Output is correct
36 Correct 304 ms 347220 KB Output is correct
37 Correct 355 ms 347312 KB Output is correct
38 Correct 285 ms 347204 KB Output is correct
39 Correct 283 ms 347160 KB Output is correct
40 Correct 299 ms 347388 KB Output is correct
41 Correct 287 ms 347152 KB Output is correct
42 Correct 310 ms 347332 KB Output is correct
43 Correct 324 ms 347204 KB Output is correct
44 Correct 296 ms 347208 KB Output is correct
45 Correct 285 ms 347148 KB Output is correct
46 Correct 294 ms 347204 KB Output is correct
47 Correct 207 ms 231520 KB Output is correct
48 Correct 273 ms 270228 KB Output is correct
49 Correct 258 ms 308672 KB Output is correct
50 Correct 286 ms 347164 KB Output is correct
51 Correct 30 ms 34892 KB Output is correct
52 Correct 289 ms 347164 KB Output is correct