#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define mp make_pair
#define ll long long
#define ii pair<int, int>
#define pl pair<ll, ll>
#define f1 first
#define s2 second
const int MAX = 25e5 + 7;
const int mod = 1e9 + 7;
inline int add(ll x, ll y) { return (int)((x + y) % mod); }
inline int mul(ll x, ll y) { return (int)((x * y) % mod); }
struct Item {int mx, l, r; };
ll dp[MAX];
static int dq_l = 1, dq_r = 0;
Item dq[MAX];
int solve(int n, int k, int* v){
priority_queue<ll, vector<ll>, greater<ll>> pq, pending;
dp[0] = v[0];
dq[++dq_r] = {v[0], 0, 0};
pq.push(v[0]);
for (int i = 1; i < n; ++i)
{
//remove the front
if (dq_l <= dq_r && dq[dq_l].l <= i-k)
{
//int mx = dq[dq_l].mx, l = dq[dq_l].mx, r = dq[dq_l].r;
auto [mx, l, r] = dq[dq_l];
dq_l++;
pending.push(mx + (l ? dp[l-1] : 0));
if (r > i-k)
{
dq[--dq_l] = {mx, i-k+1, r};
pq.push(mx + (i-k+1 ? dp[i-k] : 0));
}
}
int cur_l = i, cur_r = i;
while (dq_l <= dq_r && dq[dq_r].mx <= v[i])
{
//int mx = dq[dq_r].mx, l = dq[dq_r].l, r = dq[dq_r].r;
auto [mx, l, r] = dq[dq_r];
dq_r--;
pending.push(mx + (l ? dp[l-1] : 0));
cur_l = l;
}
dq[++dq_r] = {v[i], cur_l, cur_r};
pq.push(v[i] + (cur_l ? dp[cur_l-1] : 0));
while (!pending.empty())
{
if (pq.top() > pending.top())
pending.pop();
else if (pq.top() < pending.top())
break;
else pq.pop(), pending.pop();
}
dp[i] = pq.top();
}
int pwr = 1, ans = 0;
for (int i = n-1; i >= 0; --i)
ans = add(ans, mul(dp[i]%mod, pwr)), pwr = mul(pwr, 23);
return ans;
}
/*
for (int i = 0; i < n; ++i)
{
dp[i] = 1e18;
int mx = 0;
for (int j = i; j >= max(0, i-k+1); --j)
{
mx = max(mx, v[j]);
dp[i] = min(dp[i], mx + (j ? dp[j-1] : 0));
}
}
*/
Compilation message
peru.cpp: In function 'int solve(int, int, int*)':
peru.cpp:39:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [mx, l, r] = dq[dq_l];
| ^
peru.cpp:54:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | auto [mx, l, r] = dq[dq_r];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
372 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 |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
372 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 |
340 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 |
103 ms |
7332 KB |
Output is correct |
16 |
Correct |
106 ms |
6996 KB |
Output is correct |
17 |
Correct |
92 ms |
7884 KB |
Output is correct |
18 |
Correct |
131 ms |
6464 KB |
Output is correct |
19 |
Correct |
146 ms |
6460 KB |
Output is correct |
20 |
Correct |
152 ms |
6552 KB |
Output is correct |
21 |
Correct |
109 ms |
7464 KB |
Output is correct |
22 |
Correct |
102 ms |
7740 KB |
Output is correct |
23 |
Correct |
107 ms |
7696 KB |
Output is correct |
24 |
Correct |
103 ms |
8632 KB |
Output is correct |
25 |
Correct |
102 ms |
8860 KB |
Output is correct |
26 |
Correct |
101 ms |
7240 KB |
Output is correct |
27 |
Correct |
110 ms |
6700 KB |
Output is correct |
28 |
Correct |
114 ms |
6740 KB |
Output is correct |
29 |
Correct |
124 ms |
6768 KB |
Output is correct |
30 |
Correct |
113 ms |
7148 KB |
Output is correct |
31 |
Correct |
93 ms |
8644 KB |
Output is correct |
32 |
Correct |
111 ms |
7256 KB |
Output is correct |
33 |
Correct |
95 ms |
8260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
7332 KB |
Output is correct |
2 |
Correct |
106 ms |
6996 KB |
Output is correct |
3 |
Correct |
92 ms |
7884 KB |
Output is correct |
4 |
Correct |
131 ms |
6464 KB |
Output is correct |
5 |
Correct |
146 ms |
6460 KB |
Output is correct |
6 |
Correct |
152 ms |
6552 KB |
Output is correct |
7 |
Correct |
109 ms |
7464 KB |
Output is correct |
8 |
Correct |
102 ms |
7740 KB |
Output is correct |
9 |
Correct |
107 ms |
7696 KB |
Output is correct |
10 |
Correct |
103 ms |
8632 KB |
Output is correct |
11 |
Correct |
102 ms |
8860 KB |
Output is correct |
12 |
Correct |
101 ms |
7240 KB |
Output is correct |
13 |
Correct |
110 ms |
6700 KB |
Output is correct |
14 |
Correct |
114 ms |
6740 KB |
Output is correct |
15 |
Correct |
124 ms |
6768 KB |
Output is correct |
16 |
Correct |
113 ms |
7148 KB |
Output is correct |
17 |
Correct |
93 ms |
8644 KB |
Output is correct |
18 |
Correct |
111 ms |
7256 KB |
Output is correct |
19 |
Correct |
95 ms |
8260 KB |
Output is correct |
20 |
Correct |
1 ms |
372 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 |
340 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 |
Execution timed out |
653 ms |
36532 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |