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"
#ifdef _LOCAL_
#include "grader.cpp"
#endif // _LOCAL_
#include <vector>
#include <deque>
using namespace std;
int rmq(int l, int r, const int *a)
{
int maxVal = a[l];
for(int i = l+1;i<=r;i++) maxVal = max(maxVal, a[i]);
return maxVal;
}
int convertToAns(const vector<long long> &dp)
{
long long p23 = 1, ans = 0;
const long long mod = 1e9 + 7;
for(int i = dp.size()-1;i>=0;i--)
{
ans = (ans + (dp[i]%mod)*p23)%mod;
p23 = (p23*23)%mod;
}
return ans;
}
int solve(int n, int k, int* a)
{
vector <long long> dp(n);
dp[0] = a[0];
deque <int> dq = {0};
long long prefMaxVal = a[0];
for(int i = 1;i<n;i++)
{
dp[i] = 1e18;
prefMaxVal = max(prefMaxVal, (long long)a[i]);
if(i<k) dp[i] = min(dp[i], prefMaxVal);
while(dq.empty()==false && a[dq.back()]<=a[i]) dq.pop_back();
dq.push_back(i);
while(dq.empty()==false && i-dq.front()+1>k) dq.pop_front();
int last = i - k;
for(int j: dq)
{
if(last>=0) dp[i] = min(dp[i], dp[last] + a[j]);
last = j;
}
}
return convertToAns(dp);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |