#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> opt;
long long prefMaxVal = a[0];
for(int i = 1;i<n;i++)
{
while(opt.empty()==false && dp[opt.back()]+rmq(opt.back()+1, n-1, a)>=dp[i-1]+rmq(i, n-1, a))
{
//cout << "pop_back: " << opt.back() << '\n';
opt.pop_back();
}
opt.push_back(i-1);
while(opt.empty()==false && i-opt.front()>k) opt.pop_front();
//while(opt.size()>=2 && dp[opt[0]]+rmq(opt[0]+1, i, a)>=dp[opt[1]]+rmq(opt[1]+1, i, a)) opt.pop_front();
//cout << i << ": ";
//for(int x: opt) cout << x << " ";
//cout << '\n';
dp[i] = 1e18;
prefMaxVal = max(prefMaxVal, (long long)a[i]);
if(i<k) dp[i] = min(dp[i], prefMaxVal);
for(int j: opt) dp[i] = min(dp[i], dp[j] + rmq(j+1, i, a));
//cout << "dp[" << i << "] = " << dp[i] << '\n';
}
return convertToAns(dp);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
316 KB |
Output is correct |
4 |
Correct |
7 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
468 KB |
Output is correct |
7 |
Correct |
8 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
316 KB |
Output is correct |
9 |
Correct |
9 ms |
364 KB |
Output is correct |
10 |
Correct |
9 ms |
368 KB |
Output is correct |
11 |
Correct |
8 ms |
368 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
316 KB |
Output is correct |
4 |
Correct |
7 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
468 KB |
Output is correct |
7 |
Correct |
8 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
316 KB |
Output is correct |
9 |
Correct |
9 ms |
364 KB |
Output is correct |
10 |
Correct |
9 ms |
368 KB |
Output is correct |
11 |
Correct |
8 ms |
368 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
312 KB |
Output is correct |
15 |
Execution timed out |
1052 ms |
9292 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1052 ms |
9292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |