#include "peru.h"
#ifdef _LOCAL_
#include "grader.cpp"
#endif // _LOCAL_
#include <cassert>
#include <vector>
#include <deque>
#include <set>
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};
deque <pair <long long, int>> values;
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 && i-dq.front()>=k)
{
int j = dq.front();
dq.pop_front();
if(dq.empty()==false)
{
if(values.empty()==false && values.front()==make_pair(dp[j]+a[dq.front()], j)) values.pop_front();
}
}
while(dq.empty()==false && a[dq.back()]<=a[i])
{
if(dq.size()>=2)
{
if(values.empty()==false && values.back()==make_pair(dp[*prev(prev(dq.end()))] + a[dq.back()], *prev(prev(dq.end()))))
values.pop_back();
}
dq.pop_back();
}
if(dq.empty()==false)
{
while(values.empty()==false && values.back().first>dp[dq.back()]+a[i])
values.pop_back();
values.push_back(make_pair(dp[dq.back()]+a[i], dq.back()));
}
dq.push_back(i);
int last = max(i-k, 0);
if(dq.front()>last) dp[i] = min(dp[i], dp[last] + a[dq.front()]);
if(values.empty()==false) dp[i] = min(dp[i], values.front().first);
/*
cout << "dq: ";for(int x: dq) cout << x << " ";
cout << '\n';
cout << "values";for(auto item: values) cout << "(" << item.first << ", " << item.second << ") ";
cout << '\n';
cout << "dp[" << i << "] = " << dp[i] << '\n';
*/
}
return convertToAns(dp);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |