답안 #93339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
93339 2019-01-07T15:28:57 Z 3zp K개의 묶음 (IZhO14_blocks) C++14
0 / 100
21 ms 11484 KB
#include<bits/stdc++.h>
using namespace std;
const int mn = 100009, mk = 109;
int f[mn][mk], n, k;
int l[mn], r[mn], a[mn];
vector<int> v[mn];
int dp[mn][mk];
void upd(int i, int j, int d){
    i = n + 1 - i;
    while(i <= n + 1){
        f[i][j] = min(f[i][j], d);
        i += (i & -i);
    }
}
int cnt(int i, int j){
    i = n + 1 - i;
    int ans = 1e9;
    while(i){
        ans = min(ans, f[i][j]);
        i -= (i & -i);
    }
    return ans;
}
main(){
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    stack<int> S;
    for(int i = 1; i <= n; i++){
        while(S.size() && a[S.top()] < a[i]) 
            S.pop();
        if(!S.size()) l[i] = 1;
        else l[i] = S.top() + 1;
        S.push(i);
    }
    while(S.size()) S.pop();
    for(int i = n; i >= 1; i--){
        while(S.size() && a[S.top()] < a[i])
            S.pop();
        if(!S.size()) r[i] = n;
        else r[i] = S.top() - 1;
        S.push(i);
    }
    for(int i = 1; i <= n; i++){
        v[l[i]].push_back(i);
        
    }
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= k; j++){
            dp[i][j] = 1e9;
            f[i+1][j] = 1e9;
        }
    }
    dp[0][0] = 0;
    upd(0, 0, 0);
    for(int i = 1; i <= n; i++){
        for(int x : v[i]){
           for(int j = k; j >= 1; j--){
             int s = cnt(i - 1, j - 1);
                dp[r[x]][j] = min(dp[r[x]][j], 
                               a[x] + s);
                upd(r[x], j, dp[r[x]][j]);
            }
        }
    }
    cout << dp[n][k] << endl;
    return 0;
}

Compilation message

blocks.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 3 ms 2552 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Incorrect 4 ms 2680 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Incorrect 3 ms 2684 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 11484 KB Output isn't correct
2 Halted 0 ms 0 KB -