답안 #873939

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873939 2023-11-16T04:48:06 Z 8pete8 K개의 묶음 (IZhO14_blocks) C++14
0 / 100
1 ms 4444 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include <iomanip>  
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<pii,pii>
#define vi vector<int>
#define pb push_back
//#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define int long long
#define double long double
const int mxn=1e5,inf=1e18;
int seg[mxn+10],n,dp[mxn+10][101],l[mxn+10],r[mxn+10],v[mxn+10],k;
int qry(int l,int r){
    int ans=inf;
    for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
        if(l&1)ans=min(ans,seg[l++]);
        if(!(r&1))ans=min(ans,seg[r--]);
    }
    return ans;
}
void build(){for(int i=n-1;i>=0;i--)seg[i]=min(seg[i<<1],seg[(i<<1)^1]);}
int32_t main(){
    fastio
    cin>>n>>k;
    for(int i=0;i<n;i++)cin>>v[i];
    stack<int>st;
    for(int i=0;i<n;i++){
        while(!st.empty()&&v[st.top()]<=v[i])st.pop();
        if(!st.empty())l[i]=st.top();
        st.push(i);
    }
    while(!st.empty())st.pop();
    for(int i=n-1;i>=0;i--){
        while(!st.empty()&&v[st.top()]<=v[i])st.pop();
        if(!st.empty())r[i]=st.top();
        else r[i]=n;
        st.push(i);
    }
    for(int i=0;i<2*n;i++)seg[i]=inf;
    seg[n]=0;
    build();
    for(int j=1;j<=k;j++){
        priority_queue<pii,vector<pii>,greater<pii>>pq;
        for(int i=0;i<n;i++)dp[i][j]=inf;
        for(int i=0;i<n;i++){
            int g=qry(l[i],i)+v[i];
            pq.push({g,r[i]});
            while(!pq.empty()&&(pq.top().s<=i))pq.pop();
            if(!pq.empty())dp[i][j]=pq.top().f;
        }
        for(int i=0;i<n;i++)seg[i+n]=dp[i][j];
        build();
    }
    cout<<dp[n-1][k];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -