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;
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
int inf=2000000007;
ll infl=1000000000000000007;
ll mod=1000000007;
ll mod1=998244353;
const int N=2500007;
//ll DP1[N];
ll DP[N];
int a[N];
int solve(int n,int k,int* v)
{
FOR(i,1,n) a[i]=v[i-1];
a[0]=inf,a[n+1]=inf;
deque<pair<ll,int>>Q;
vector<pair<int,int>>X;
X.pb({inf,0});
FOR(i,1,n+1)
{
int last=-1;
while(X.back().st<a[i])
{
if(last!=-1&&i-1-X.back().nd<=k) DP[i-1]=min(DP[i-1],DP[X.back().nd]+last);
last=X.back().st;
X.pop_back();
}
if(last!=-1&&i-1-X.back().nd<=k) DP[i-1]=min(DP[i-1],DP[X.back().nd]+last);
ROF(j,i-2,1)
{
if(DP[j]<=DP[i-1]) break;
DP[j]=DP[i-1];
}
X.pb({a[i],i});
if(i==n+1) break;
DP[i]=infl;
if(sz(Q)>0&&Q[0].nd<=i-k) Q.pop_front();
while(sz(Q)>0&&Q.back().st<a[i]) Q.pop_back();
Q.pb({a[i],i});
DP[i]=DP[max(0,i-k)]+Q[0].st;
}
/*FOR(i,1,n)
{
DP1[i]=infl;
int mx=0;
ROF(j,i,1)
{
if(i-j+1>k) break;
mx=max(mx,a[j]);
DP1[i]=min(DP1[i],DP1[j-1]+mx);
}
}
FOR(i,1,n) debug(DP[i],DP1[i]);*/
ll h=0;
FOR(i,1,n) h=(23*h+DP[i])%mod;
return (int)h;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |