#include <bits/stdc++.h>
//#define home
#ifndef home
#include "peru.h"
#endif // home
using namespace std;
const long long oo = LLONG_MAX;
const long long Mod = 1e9 + 7;
int n,k;
long long v[2500005];
long long dp[2500005];
int tip[2500005];
void precalc()
{
deque<int> d;
for(int i=1; i<=n; i++)
{
while(!d.empty() && v[i]>v[d.back()])
{
tip[d.back()] = 1;
d.pop_back();
}
d.push_back(i);
if(d.front() < i - k)
{
tip[d.front()] = 2;
d.pop_front();
}
}
for(int i=1;i<=n;i++)
{
if(!tip[i])
{
tip[i] = 2;
}
}
}
int solve(int N, int K, int s[])
{
n = N;
k = K;
for(int i=1; i<=n; i++)
{
v[i] = s[i-1];
}
precalc();
deque<int> d;
deque<pair<long long,int>> d_left;
stack<pair<long long,int>> st_right;
long long poz_front = 0,val_front = 0;
long long Max = 0;
for(int i=1; i<=n; i++)
{
if(i==n)
{
i = n;
}
while(!d.empty() && v[i] > v[d.back()])
{
d.pop_back();
st_right.pop();
}
if(st_right.empty())
{
poz_front = 0;
}
if(tip[i]==1)
{
if(st_right.empty())
{
st_right.push({oo,i});
if(!d.empty())
{
val_front = dp[d.back()] + v[i];
poz_front = d.back();
}
}
else
{
st_right.push({min(st_right.top().first,dp[d.back()] + v[i]),i});
}
}
else
{
while(!d_left.empty() && (v[i] + dp[d.back()]) < d_left.back().first)
{
d_left.pop_back();
}
if(!d.empty())
{
d_left.push_back({v[i] + dp[d.back()],d.back()});
}
if(!d_left.empty() && d_left.front().second < i - k)
{
d_left.pop_front();
}
}
d.push_back(i);
if(d.front() < i - k)
{
d.pop_front();
}
Max = max(Max,v[i]);
if(i<=k)
{
dp[i] = Max;
continue;
}
if(d.front() >= i - k + 1)
{
dp[i] = v[d.front()] + dp[i - k];
}
else
{
dp[i] = v[d[1]] + dp[i - k];
}
if(poz_front >= i - k && poz_front <= d.front())
{
dp[i] = min(dp[i],val_front);
}
if(!d_left.empty())
{
dp[i] = min(dp[i],d_left.front().first);
}
if(!st_right.empty())
{
dp[i] = min(dp[i],st_right.top().first);
}
}
for(int i=k+1;i<=n;i++)
{
cout<<dp[i]<<' ';
}
/*
long long rez = 0, put = 1;
for(int i=n;i>=1;i--)
{
dp[i] %= Mod;
rez += 1LL * dp[i] * put % Mod;
rez %= Mod;
put = 1LL * put * 23 % Mod;
}
return rez;*/
}
#ifdef home
int nn;
int vv[100005];
signed main()
{
freopen("peru.in","r",stdin);
freopen("peru.out","w",stdout);
cin>>n>>k;
for(int i=0; i<n; i++)
{
cin>>vv[i];
}
solve(n,k,vv);
// cout<<solve(n,k,vv)<<'\n';
return 0;
}
#endif // home
Compilation message
peru.cpp: In function 'int solve(int, int, int*)':
peru.cpp:156:1: warning: no return statement in function returning non-void [-Wreturn-type]
156 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
548 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
548 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
548 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |