이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const ll inf = 4e18;
const ll mod = 1e9+7;
const ll p = 23;
const int mxn = 250001;
int sparse[21][mxn];
int arr[mxn];
deque<int> dq;
pll dp[mxn];
void build(int N){
for(int i = 0;i<=N;i++){
sparse[0][i] = arr[i];
}
for(int i = 1;i<21;i++){
for(int j = 0;j+(1<<i)-1<=N;j++){
sparse[i][j] = max(sparse[i-1][j],sparse[i-1][j+(1<<(i-1))]);
}
}
return;
}
int getval(int l,int r){
int h = __lg(r-l+1);
return max(sparse[h][l],sparse[h][r-(1<<h)+1]);
}
const int magic = 100;
int solve(int N,int K,int* S){
for(int i = N;i>=1;i--)arr[i] = S[i-1];
arr[0] = 0;
build(N);
dp[0] = make_pair(0,0);
ll re = 0;
for(int i = 1;i<=N;i++){
dp[i] = make_pair(inf,i);
for(int j = i-1;i-j<=K&&j>=0&&i-j<=magic;j--){
dp[i] = min(dp[i],make_pair(dp[j].fs+getval(j+1,i),1ll*j));
}
int C = 0;
for(int j = max(dp[i-1].sc,1ll*i-K);C<=magic&&j<i;C++,j++){
dp[i] = min(dp[i],make_pair(dp[j].fs+getval(j+1,i),1ll*j));
}
re = (re*p+dp[i].fs)%mod;
}
//for(int i = 1;i<=N;i++)cout<<dp[i].fs<<' ';cout<<endl;
return re;
}
/*
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i = 0;i<n;i++)cin>>arr[i];
cout<<solve(n,k,(int*)arr);
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |