#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("popcnt,avx2")
#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
using ll = long long;
const int N=2005;
ll dp[N];
int solve(int n, int k, int* v){
const int mod = 1e9+7;
#define int long long
deque<pi> s;
deque<pi> z;
ll ans=0;
forn(i,n) {
while (s.size() && s[0].s<=i-k) s.pop_front();
while (z.size() && z[0].s<=i-k) z.pop_front();
while (s.size() && s.back().f < v[i]) {
s.pop_back();
}
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) {
if (z.size()) if (dp[i-k]+s[0].f <= z.front().f) z.push_front({dp[i-k]+s[0].f,i-k+1});
}
//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';
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]);
//assert(z.size());
if (z.size()) dp[i] = min(dp[i],1ll*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
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |