This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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()) {
s.pb({v[i],i});
int t = s.size()-2;
while (z.size() && z.back().s > s[t].s) z.pop_back();
while (t>0 && (dp[s[t].s]+s[t+1].f >= dp[s[t-1].s]+s[t].f)) {
--t;
}
if (s[t].s<i-1) {
while (z.size() && s[t].s+1 < z.back().s) z.pop_back();
while (z.size() && z.back().f > dp[s[t].s]+s[t+1].f) z.pop_back();
z.push_back({dp[s[t].s]+s[t+1].f,s[t].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<<" "<<z.front().f<<'\n';
if (z.size()) if (dp[R]+s[ptr].f < z.front().f) {
while (z.size() && (R+1>=z.front().s)) z.pop_front();
z.push_front({dp[R]+s[ptr].f,R+1});
}
}
//for(auto&x:z) cout<<x.f<<','<<x.s<<" "; cout<<'\n';
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... |