답안 #827333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827333 2023-08-16T11:18:08 Z YassineBenYounes Liteh and Newfiteh (INOI20_litehfiteh) C++17
100 / 100
762 ms 399136 KB
#include<bits/stdc++.h>
using namespace std;
void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}
typedef long long ll;
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
#define vii vector<pii>
#define pb push_back
#define vi vector<int>
const int mx = 1e5+5;
const ll inf = 1e11;
const int LOG = 22;
 
int n;
int arr[mx];
ll dp[mx];
ll dp2[mx][LOG][LOG];
int st[mx][LOG];
int lg[mx];
 
void pre_log(int n){
    lg[1] = 0;
    for(int i = 2; i <= n;i++){
        lg[i] = lg[i/2]+1;
    }
}
 
void build(){
    for(int i = 0; i < n;i++){
        st[i][0] = arr[i];
    }
    for(int j = 1;j < LOG;j++){
        for(int i = 0; i < n;i++){
            int pos = i + (1 << (j-1));
            if(pos >= n)continue;
            st[i][j] = min(st[i][j-1], st[pos][j-1]);
        }
    }
}
 
int min_query(int a, int b){
    int sz = b-a+1;
    int k = lg[sz];
    return min(st[a][k], st[b-(1<<k)+1][k]);
}
 
ll f(int l, int j, int c){
    if(dp2[l][j][c] != -1)return dp2[l][j][c];
    int r = l + (1 << j)-1;
    ll ans = 0;
    int k = min_query(l, r);
    int add = 0;
    if((k-c) != 0){
        add++;
        ans++;
    }
    if(l == r){
        if((arr[l]-c-add) != 0)return dp2[l][j][c] = inf;
        return dp2[l][j][c] = ans;
    }
    int md = (l+r)/2;
    ans += f(l, j-1, c+add) + f(md+1, j-1, c+add);
    return dp2[l][j][c] = ans;
}
 
int32_t main(){
    //init();
    cin.tie();cout.tie();ios::sync_with_stdio(false);
    cin >> n;
    pre_log(n+1);
    for(int i = 0; i < n;i++){
        cin >> arr[i];
    }
    build();
    for(int i = 0; i <= n;i++){
        dp[i] = inf;
    }
    memset(dp2, -1, sizeof dp2);
    dp[0] = 0;
    for(int ind = 0; ind < n;ind++){
        int k = 0;
        for(int i = 1; ind+i-1 < n; i*=2){
            dp[ind+i] = min(dp[ind+i], dp[ind] + f(ind, k, 0));
            k++;
        }
    }
    cout << (dp[n] >= inf ? -1 : dp[n]) << endl;
}

Compilation message

Main.cpp: In function 'void init()':
Main.cpp:6:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:8:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 379236 KB Output is correct
2 Correct 111 ms 379320 KB Output is correct
3 Correct 114 ms 379248 KB Output is correct
4 Correct 115 ms 379236 KB Output is correct
5 Correct 113 ms 379304 KB Output is correct
6 Correct 117 ms 379208 KB Output is correct
7 Correct 112 ms 379204 KB Output is correct
8 Correct 118 ms 379308 KB Output is correct
9 Correct 112 ms 379280 KB Output is correct
10 Correct 112 ms 379204 KB Output is correct
11 Correct 114 ms 379304 KB Output is correct
12 Correct 114 ms 379316 KB Output is correct
13 Correct 113 ms 379212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 379236 KB Output is correct
2 Correct 111 ms 379320 KB Output is correct
3 Correct 114 ms 379248 KB Output is correct
4 Correct 115 ms 379236 KB Output is correct
5 Correct 113 ms 379304 KB Output is correct
6 Correct 117 ms 379208 KB Output is correct
7 Correct 112 ms 379204 KB Output is correct
8 Correct 118 ms 379308 KB Output is correct
9 Correct 112 ms 379280 KB Output is correct
10 Correct 112 ms 379204 KB Output is correct
11 Correct 114 ms 379304 KB Output is correct
12 Correct 114 ms 379316 KB Output is correct
13 Correct 113 ms 379212 KB Output is correct
14 Correct 113 ms 379204 KB Output is correct
15 Correct 113 ms 379200 KB Output is correct
16 Correct 112 ms 379184 KB Output is correct
17 Correct 116 ms 379228 KB Output is correct
18 Correct 116 ms 379284 KB Output is correct
19 Correct 115 ms 379404 KB Output is correct
20 Correct 127 ms 381160 KB Output is correct
21 Correct 126 ms 381124 KB Output is correct
22 Correct 377 ms 398780 KB Output is correct
23 Correct 484 ms 399136 KB Output is correct
24 Correct 462 ms 398948 KB Output is correct
25 Correct 114 ms 379212 KB Output is correct
26 Correct 115 ms 379192 KB Output is correct
27 Correct 116 ms 379160 KB Output is correct
28 Correct 113 ms 379368 KB Output is correct
29 Correct 129 ms 381068 KB Output is correct
30 Correct 129 ms 381132 KB Output is correct
31 Correct 132 ms 381132 KB Output is correct
32 Correct 129 ms 381156 KB Output is correct
33 Correct 374 ms 398952 KB Output is correct
34 Correct 698 ms 399036 KB Output is correct
35 Correct 593 ms 399024 KB Output is correct
36 Correct 388 ms 398956 KB Output is correct
37 Correct 598 ms 399052 KB Output is correct
38 Correct 403 ms 398944 KB Output is correct
39 Correct 372 ms 398948 KB Output is correct
40 Correct 593 ms 399016 KB Output is correct
41 Correct 369 ms 398848 KB Output is correct
42 Correct 762 ms 399048 KB Output is correct
43 Correct 390 ms 398956 KB Output is correct
44 Correct 387 ms 398956 KB Output is correct
45 Correct 393 ms 398956 KB Output is correct
46 Correct 398 ms 398960 KB Output is correct
47 Correct 313 ms 392340 KB Output is correct
48 Correct 280 ms 394572 KB Output is correct
49 Correct 334 ms 396772 KB Output is correct
50 Correct 406 ms 398956 KB Output is correct
51 Correct 126 ms 381088 KB Output is correct
52 Correct 373 ms 398956 KB Output is correct