#include "peru.h"
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e18;
const int N = 25e5 + 10;
const int MOD = 1e9 + 7;
struct obj{
ll sum;
int arr;
int ind;
};
int cntb = 0;
ll dp[N];
deque<obj> d;
stack<ll> l, r;
int add(ll a, ll b){
return (a + b) % MOD;
}
int mult(ll a, ll b){
return a * b % MOD;
}
ll opt(){
ll ret = INF;
if(!l.empty()) ret = min(ret, l.top());
if(!r.empty()) ret = min(ret, r.top());
return ret;
}
void push(stack<ll> &s, ll val){
if(s.empty()) s.push(val);
else s.push(min(val, s.top()));
}
void build(){
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
int siz = d.size();
int m = d.size() / 2;
vector<obj> v;
for(int i = 0; i < m; i++){
v.push_back(d.front());
d.pop_front();
}
while(!v.empty()){
push(l, v.back().sum);
d.push_front(v.back());
v.pop_back();
}
for(int i = 0; i < siz - m; i++){
v.push_back(d.back());
d.pop_back();
}
while(!v.empty()){
push(r, v.back().sum);
d.push_back(v.back());
v.pop_back();
}
}
obj getobj(ll sum, int arr, int ind){
obj ret;
ret.sum = sum;
ret.arr = arr;
ret.ind = ind;
return ret;
}
int solve(int n, int k, int* arr){
for(int i = 1; i <= n; i++){
if(i == 1){
dp[i] = arr[i - 1];
d.push_back(getobj(dp[i - 1] + arr[i - 1], arr[i - 1], i));
push(r, dp[i - 1] + arr[i - 1]);
} else {
int dpind = i;
while(!d.empty() && d.back().arr <= arr[i - 1]){
dpind = d.back().ind;
d.pop_back();
if(r.empty()) build();
else r.pop();
}
d.push_back(getobj(dp[dpind - 1] + arr[i - 1], arr[i - 1], dpind));
push(r, dp[dpind - 1] + arr[i - 1]);
if(i > k){
ll arrmax = d.front().arr;
if(d.front().ind <= i - k){
d.pop_front();
if(l.empty()) build();
else l.pop();
if(d.empty()){
d.push_front(getobj(dp[i - k] + arrmax, arrmax, i - k + 1));
push(l, dp[i - k] + arrmax);
} else if(d.front().ind > i - k + 1){
d.push_front(getobj(dp[i - k] + arrmax, arrmax, i - k + 1));
push(l, dp[i - k] + arrmax);
}
}
}
dp[i] = opt();
}
}
for(int i = 1; i <= n; i++) dp[i] %= MOD;
ll ans = 0, m = 1;
for(int i = n; i > 0; i--){
ans = add(ans, mult(dp[i], m));
m = mult(m, 23);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 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 |
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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
27 ms |
9176 KB |
Output is correct |
16 |
Correct |
27 ms |
9152 KB |
Output is correct |
17 |
Correct |
28 ms |
9224 KB |
Output is correct |
18 |
Correct |
32 ms |
9188 KB |
Output is correct |
19 |
Correct |
28 ms |
9160 KB |
Output is correct |
20 |
Correct |
28 ms |
9160 KB |
Output is correct |
21 |
Correct |
32 ms |
9152 KB |
Output is correct |
22 |
Correct |
30 ms |
9232 KB |
Output is correct |
23 |
Correct |
28 ms |
9348 KB |
Output is correct |
24 |
Correct |
29 ms |
9200 KB |
Output is correct |
25 |
Correct |
29 ms |
9196 KB |
Output is correct |
26 |
Correct |
33 ms |
9228 KB |
Output is correct |
27 |
Correct |
25 ms |
9256 KB |
Output is correct |
28 |
Correct |
26 ms |
9280 KB |
Output is correct |
29 |
Correct |
30 ms |
9460 KB |
Output is correct |
30 |
Correct |
28 ms |
9152 KB |
Output is correct |
31 |
Correct |
30 ms |
9372 KB |
Output is correct |
32 |
Correct |
25 ms |
9272 KB |
Output is correct |
33 |
Correct |
31 ms |
9156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
9176 KB |
Output is correct |
2 |
Correct |
27 ms |
9152 KB |
Output is correct |
3 |
Correct |
28 ms |
9224 KB |
Output is correct |
4 |
Correct |
32 ms |
9188 KB |
Output is correct |
5 |
Correct |
28 ms |
9160 KB |
Output is correct |
6 |
Correct |
28 ms |
9160 KB |
Output is correct |
7 |
Correct |
32 ms |
9152 KB |
Output is correct |
8 |
Correct |
30 ms |
9232 KB |
Output is correct |
9 |
Correct |
28 ms |
9348 KB |
Output is correct |
10 |
Correct |
29 ms |
9200 KB |
Output is correct |
11 |
Correct |
29 ms |
9196 KB |
Output is correct |
12 |
Correct |
33 ms |
9228 KB |
Output is correct |
13 |
Correct |
25 ms |
9256 KB |
Output is correct |
14 |
Correct |
26 ms |
9280 KB |
Output is correct |
15 |
Correct |
30 ms |
9460 KB |
Output is correct |
16 |
Correct |
28 ms |
9152 KB |
Output is correct |
17 |
Correct |
30 ms |
9372 KB |
Output is correct |
18 |
Correct |
25 ms |
9272 KB |
Output is correct |
19 |
Correct |
31 ms |
9156 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 |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
165 ms |
35328 KB |
Output is correct |
35 |
Correct |
155 ms |
35696 KB |
Output is correct |
36 |
Correct |
148 ms |
35424 KB |
Output is correct |
37 |
Correct |
155 ms |
35400 KB |
Output is correct |
38 |
Correct |
152 ms |
35508 KB |
Output is correct |
39 |
Correct |
152 ms |
35656 KB |
Output is correct |
40 |
Correct |
152 ms |
35572 KB |
Output is correct |
41 |
Correct |
160 ms |
35568 KB |
Output is correct |
42 |
Correct |
155 ms |
35608 KB |
Output is correct |
43 |
Correct |
61 ms |
21816 KB |
Output is correct |
44 |
Correct |
102 ms |
33356 KB |
Output is correct |
45 |
Correct |
100 ms |
33460 KB |
Output is correct |
46 |
Correct |
102 ms |
33356 KB |
Output is correct |
47 |
Correct |
148 ms |
35760 KB |
Output is correct |
48 |
Correct |
152 ms |
35972 KB |
Output is correct |
49 |
Correct |
154 ms |
36048 KB |
Output is correct |
50 |
Correct |
147 ms |
36616 KB |
Output is correct |
51 |
Correct |
147 ms |
36264 KB |
Output is correct |
52 |
Correct |
156 ms |
37048 KB |
Output is correct |
53 |
Correct |
142 ms |
36612 KB |
Output is correct |
54 |
Correct |
173 ms |
35484 KB |
Output is correct |
55 |
Correct |
140 ms |
35364 KB |
Output is correct |
56 |
Correct |
145 ms |
35344 KB |
Output is correct |
57 |
Correct |
146 ms |
35280 KB |
Output is correct |
58 |
Correct |
140 ms |
35188 KB |
Output is correct |
59 |
Correct |
146 ms |
35076 KB |
Output is correct |
60 |
Correct |
150 ms |
34904 KB |
Output is correct |
61 |
Correct |
159 ms |
35056 KB |
Output is correct |
62 |
Correct |
151 ms |
34544 KB |
Output is correct |
63 |
Correct |
155 ms |
33996 KB |
Output is correct |