Submission #635307

# Submission time Handle Problem Language Result Execution time Memory
635307 2022-08-26T03:58:02 Z ThMinh_ K blocks (IZhO14_blocks) C++14
0 / 100
1 ms 340 KB
#include<bits/stdc++.h>
#define forin(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
using namespace std;
const int N = 1e5 + 10, M = 1e2 + 10;
int n, k;
ll a[N], dp[N][M], sp[N][17];
void build() {
    forin(i,1,n) sp[i][0] = a[i];
    forin(j,1,log2(n)) forin(i,1,n - (1<<j) + 1) {
        sp[i][j] = max(sp[i][j - 1], sp[i + (1<<(j - 1))][j - 1]);
    }
}
ll cost(int f, int l) {
    if(f > l) return 1e16;
    int j = (int)log2(l - f + 1);
    return max(sp[f][j], sp[l - (1<<j) + 1][j]);
}
void solve(int f, int l, int from, int to, int k) {
    if(f > l) return;
    int mid = f + l >>1, p;
    ll &a = dp[mid][k];
    a = 1e16;
    forin(i,from,to) {
        ll nw = dp[i - 1][k - 1] + cost(i, mid);
        if(a > nw) {
            a = nw; p = i;
        }
    }
    solve(f, mid - 1, from, p, k);
    solve(mid + 1, l, p, to, k);
}
int main () {
    cin.tie(0)->sync_with_stdio(0);
    if(fopen("Task.inp","r")) {
        freopen("Task.inp","r",stdin);
        freopen("WA.out","w",stdout);
    }
    cin>>n>>k;
    forin(i,1,n) cin>>a[i];
    build();
    dp[0][0];
    forin(i,1,n) dp[i][0] = 1e16;
    forin(i,1,k) {
        dp[0][i] = 1e16;
        solve(1, n, 1, n, i);
    }
    cout<<dp[n][k];
}

Compilation message

blocks.cpp: In function 'void solve(int, int, int, int, int)':
blocks.cpp:21:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |     int mid = f + l >>1, p;
      |               ~~^~~
blocks.cpp: In function 'int main()':
blocks.cpp:42:12: warning: statement has no effect [-Wunused-value]
   42 |     dp[0][0];
      |     ~~~~~~~^
blocks.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen("Task.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen("WA.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
blocks.cpp: In function 'void solve(int, int, int, int, int)':
blocks.cpp:30:10: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
   30 |     solve(f, mid - 1, from, p, k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 0 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -