답안 #635308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635308 2022-08-26T03:59:05 Z ThMinh_ K개의 묶음 (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] = 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: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);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 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 -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -