#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};
multiset <long long> ms;
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)
ms.erase(ms.find(dp[j]+a[dq.front()]));
}
while(dq.empty()==false && a[dq.back()]<=a[i])
{
if(dq.size()>=2) ms.erase(ms.find(dp[*prev(prev(dq.end()))] + a[dq.back()]));
dq.pop_back();
}
if(dq.empty()==false) ms.insert(dp[dq.back()]+a[i]);
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(ms.empty()==false)
dp[i] = min(dp[i], (long long)*ms.begin());
assert(ms.size()==dq.size()-1);
/*
cout << "dq:";for(int x: dq) cout << x << " ";
cout << '\n';
cout << "ms:";for(int x: ms) cout << x << " ";
cout << '\n';
cout << "dp[" << i << "] = " << dp[i] << '\n';
*/
}
return convertToAns(dp);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
61 ms |
5300 KB |
Output is correct |
16 |
Correct |
61 ms |
6988 KB |
Output is correct |
17 |
Correct |
67 ms |
7068 KB |
Output is correct |
18 |
Correct |
44 ms |
7016 KB |
Output is correct |
19 |
Correct |
50 ms |
6952 KB |
Output is correct |
20 |
Correct |
45 ms |
6992 KB |
Output is correct |
21 |
Correct |
61 ms |
7076 KB |
Output is correct |
22 |
Correct |
76 ms |
7032 KB |
Output is correct |
23 |
Correct |
66 ms |
9292 KB |
Output is correct |
24 |
Correct |
61 ms |
9244 KB |
Output is correct |
25 |
Correct |
62 ms |
9228 KB |
Output is correct |
26 |
Correct |
59 ms |
9140 KB |
Output is correct |
27 |
Correct |
71 ms |
9196 KB |
Output is correct |
28 |
Correct |
66 ms |
9296 KB |
Output is correct |
29 |
Correct |
59 ms |
9516 KB |
Output is correct |
30 |
Correct |
59 ms |
9144 KB |
Output is correct |
31 |
Correct |
62 ms |
9136 KB |
Output is correct |
32 |
Correct |
61 ms |
9288 KB |
Output is correct |
33 |
Correct |
69 ms |
9196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
5300 KB |
Output is correct |
2 |
Correct |
61 ms |
6988 KB |
Output is correct |
3 |
Correct |
67 ms |
7068 KB |
Output is correct |
4 |
Correct |
44 ms |
7016 KB |
Output is correct |
5 |
Correct |
50 ms |
6952 KB |
Output is correct |
6 |
Correct |
45 ms |
6992 KB |
Output is correct |
7 |
Correct |
61 ms |
7076 KB |
Output is correct |
8 |
Correct |
76 ms |
7032 KB |
Output is correct |
9 |
Correct |
66 ms |
9292 KB |
Output is correct |
10 |
Correct |
61 ms |
9244 KB |
Output is correct |
11 |
Correct |
62 ms |
9228 KB |
Output is correct |
12 |
Correct |
59 ms |
9140 KB |
Output is correct |
13 |
Correct |
71 ms |
9196 KB |
Output is correct |
14 |
Correct |
66 ms |
9296 KB |
Output is correct |
15 |
Correct |
59 ms |
9516 KB |
Output is correct |
16 |
Correct |
59 ms |
9144 KB |
Output is correct |
17 |
Correct |
62 ms |
9136 KB |
Output is correct |
18 |
Correct |
61 ms |
9288 KB |
Output is correct |
19 |
Correct |
69 ms |
9196 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
400 ms |
53304 KB |
Output is correct |
35 |
Correct |
408 ms |
53648 KB |
Output is correct |
36 |
Correct |
394 ms |
53696 KB |
Output is correct |
37 |
Correct |
419 ms |
53468 KB |
Output is correct |
38 |
Correct |
406 ms |
53680 KB |
Output is correct |
39 |
Correct |
410 ms |
53716 KB |
Output is correct |
40 |
Correct |
423 ms |
53640 KB |
Output is correct |
41 |
Correct |
425 ms |
53552 KB |
Output is correct |
42 |
Correct |
422 ms |
53708 KB |
Output is correct |
43 |
Correct |
155 ms |
21844 KB |
Output is correct |
44 |
Correct |
156 ms |
33348 KB |
Output is correct |
45 |
Correct |
148 ms |
33292 KB |
Output is correct |
46 |
Correct |
157 ms |
33368 KB |
Output is correct |
47 |
Correct |
431 ms |
55700 KB |
Output is correct |
48 |
Correct |
416 ms |
56032 KB |
Output is correct |
49 |
Correct |
447 ms |
56220 KB |
Output is correct |
50 |
Correct |
401 ms |
57252 KB |
Output is correct |
51 |
Correct |
415 ms |
56780 KB |
Output is correct |
52 |
Correct |
415 ms |
59032 KB |
Output is correct |
53 |
Correct |
423 ms |
57804 KB |
Output is correct |
54 |
Correct |
421 ms |
53468 KB |
Output is correct |
55 |
Correct |
367 ms |
53460 KB |
Output is correct |
56 |
Correct |
381 ms |
53548 KB |
Output is correct |
57 |
Correct |
411 ms |
53484 KB |
Output is correct |
58 |
Correct |
407 ms |
53520 KB |
Output is correct |
59 |
Correct |
390 ms |
53468 KB |
Output is correct |
60 |
Correct |
447 ms |
53480 KB |
Output is correct |
61 |
Correct |
466 ms |
55836 KB |
Output is correct |
62 |
Correct |
455 ms |
55880 KB |
Output is correct |
63 |
Correct |
434 ms |
55476 KB |
Output is correct |