이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("popcnt,avx2")
using ll = long long;
const int N=25e5+5;
ll dp[N];
int solve(int n, int k, int* v){
const int mod = 1e9+7;
#define int long long
#define forn(i,n) for(int i=0; i<(n); ++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
deque<pi> s;
deque<pi> z;
ll ans=0;
int ptr=0;
int R=0;
forn(i,n) {
while (s.size() && s[0].s<=i-k) {
s.pop_front();
--ptr;
}
while (z.size() && z[0].s<=i-k) z.pop_front();
while (s.size() && s.back().f < v[i]) {
s.pop_back();
ptr=min(ptr,(int)s.size());
}
if (s.size()) {
while (z.size() && z.back().s > s.back().s) z.pop_back();
if (s.back().s<i-1) z.push_back({dp[s.back().s]+v[i],s.back().s+1});
} else {
while (z.size()) z.pop_back();
if (k>1 && i-k>=0) z.push_back({dp[i-k]+v[i],i-k+1});
}
s.pb({v[i],i});
if (i-k>=0) {
R=max(R,i-k);
ptr=max(ptr,0ll);
//cout<<"> "<<R<<' '<<ptr<<'\n';
while (dp[R]==dp[R+1]) ++R;
while (s[ptr].s<=R) ++ptr;
//cout<<"???? "<<R<<' '<<ptr<<' '<<s[ptr].f<<' '<<s[ptr].s<<'\n';
if (z.size()) if (dp[R]+s[ptr].f < z.front().f) z.push_front({dp[R]+s[ptr].f,R+1});
}
dp[i]=s[0].f;
if (i-k>=0) dp[i]+=dp[i-k];
if (i) dp[i]=min(dp[i],dp[i-1]+v[i]);
if (z.size()) dp[i] = min(dp[i],z[0].f);
if (i) {
while (z.size() && z.back().f > dp[i-1]+v[i]) z.pop_back();
z.pb({dp[i-1]+v[i],i});
} else {
while (z.size() && z.back().f > v[i]) z.pop_back();
z.pb({v[i],i});
}
ans=(ans*23)%mod;
ans=(ans+dp[i])%mod;
//cout<<i<<": "<<dp[i]<<'\n';
//for(auto&x:s) cout<<x.f<<','<<x.s<<" "; cout<<'\n';
//for(auto&x:z) cout<<x.f<<','<<x.s<<" "; cout<<'\n';
//cout<<'\n';
}
return ans;
#undef int
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |