답안 #556251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
556251 2022-05-02T16:09:42 Z urosk K개의 묶음 (IZhO14_blocks) C++14
0 / 100
1 ms 388 KB
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 1000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;

#define maxn 100005
#define maxk 105
#define lg 25
ll n,k;
ll a[maxn];
bool pisi = 1;
ll st[maxn][lg];
ll powi[maxn];
bool test = 0;
ll get(ll l,ll r){
    ll len = (r-l+1);
    ll k = powi[len];
    ll ans = min(st[l][k],st[r-(1<<k)+1][k]);
    return ans;
}
ll b[maxn];
ll dp[2][maxn];
ll dpmin[2][maxn];

int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> k;
    for(ll i = 1;i<=n;i++) cin >> a[i];
    stack<ll> ste;
    for(ll i = 1;i<=n;i++){
        while(sz(ste)&&a[i]>=a[ste.top()]) ste.pop();
        if(sz(ste)) b[i] = ste.top();
        ste.push(i);
    }
    for(ll j = 0;(1<<j)<=n+1;j++) powi[1<<j] = j;
    for(ll i = 1;i<=n+1;i++) if(powi[i]==0) powi[i] = powi[i-1];
    fill(dp[0],dp[0]+n+1,llinf);
    dp[0][0] = 0;
    for(ll i = 0;i<=n;i++) st[i][0] = dp[0][i];
    for(ll j = 1;j<lg;j++){
        for(ll i = 0;i+(1<<j)-1<=n;i++){
            st[i][j] = min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
    }
    //ceri(b,1,n);
    for(ll j = 1;j<=k;j++){
        dp[j&1][0] = llinf;
        //here;
        //ceri(dp[(j&1)^1],0,n);
        //ceri(dpmin[(j&1)^1],0,n);
        for(ll i = 1;i<=n;i++){
            if(b[i]==0){
                dp[j&1][i] = dpmin[(j&1)^1][i-1] + a[i];
            }else{
                dp[j&1][i] = min(dp[(j&1)^1][i-1]+a[i],dp[j&1][b[i]]);
            }
        }
        for(ll i = 0;i<=n;i++) dp[(j&1)^1][i] = dp[j&1][i];
        ll jj = (j&1);
        dpmin[jj][0] = dp[jj][0];
        for(ll i = 1;i<=n;i++) dpmin[jj][i] = min(dpmin[jj][i-1],dp[jj][i]);
    }
    //here;
    //ceri(dp[k&1],0,n);
    cout<<dp[k&1][n]<<endl;
	return 0;
}
/*
5 1
1 2 3 4 5
out: 5
5 2
1 2 3 4 5
out: 6
5 4
3 1 3 5 1
out: 10
7 4
10 7 4 1 8 4 1
10 5
5 9 2 9 4 6 9 3 7 8
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 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 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 0 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 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 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Incorrect 1 ms 340 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 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 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 0 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 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 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 0 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -