#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#include "peru.h"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 2500002;
const ll INF = (1LL << 60);
const ll HALF = (1LL << 59);
const ll MOD = 1000000007;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;
const ll LIMIT = 1000;
ll dp[NMAX];
ll a[NMAX];
ll put[NMAX];
ll r[NMAX];
class MyDeque{
queue <ll> q;
ll stiva[NMAX];
ll vf;
ll minim[NMAX];
deque <pii> d;
public:
void insertDeque(pii x){
while(d.size() && d.back().first >= x.first){
d.pop_back();
}
d.push_back(x);
}
void eraseDeque(ll x){
if(d.size() && d.front().second == x)
d.pop_front();
}
void update(ll lower_limit){
while(q.size() && q.front() < lower_limit){
eraseDeque(q.front());
q.pop();
}
}
void insertL(ll x){
ll sum = -1;
if(q.size()){
sum = a[x] + dp[q.back()];
}
if(sum != -1)
insertDeque({sum, q.back()});
vf = 0;
q.push(x);
}
void insertR(ll x){
ll sum = -1;
while(vf && a[stiva[vf]] <= a[x]){
vf--;
}
if(vf){
sum = a[x] + dp[stiva[vf]];
}
stiva[++vf] = x;
minim[0] = INF;
if(sum != -1)
minim[vf] = min(minim[vf - 1], sum);
else
minim[vf] = INF;
}
void insert(ll x){
///pot verifica chestii aici
if(r[x]){
insertR(x);
}else{
insertL(x);
}
}
ll size(){
return q.size() + vf;
}
ll front(){
if(q.size())
return q.front();
return stiva[1];
}
ll best(){
ll sol = INF;
if(vf)
sol = min(sol, minim[vf]);
if(d.size())
sol = min(sol, d.front().first);
if(q.size() && vf){
sol = min(sol, dp[q.back()] + a[stiva[1]]);
}
return sol; ///mai am de adaugat
}
}dq;
int solve(int n, int k, int* v) {
ll sol = 0;
for(ll i = 0; i < n; i++)
a[i + 1] = v[i];
put[0] = 1;
for(ll i = 1; i <= n; i++) {
put[i] = put[i - 1] * 23;
put[i] %= MOD;
}
deque <ll> sim;
for(ll i = 1; i <= n; i++){
while(sim.size() && sim.front() < i - k + 1)
sim.pop_front();
while(sim.size() && a[sim.back()] <= a[i]){
r[sim.back()] = 1;
sim.pop_back();
}
sim.push_back(i);
}
for(ll i = 1; i <= n; i++) {
dq.update(i - k + 1);
dq.insert(i);
dp[i] = INF;
if(dq.size())
dp[i] = dp[max(0LL, i - k)] + a[dq.front()]; /// i - k il prelucram separat
dp[i] = min(dp[i], dq.best());
// debug(dp[i]);
sol += ((dp[i] % MOD) * put[n - i]) % MOD;
sol %= MOD;
}
return sol;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
24 ms |
14508 KB |
Output is correct |
16 |
Correct |
24 ms |
14484 KB |
Output is correct |
17 |
Correct |
25 ms |
14468 KB |
Output is correct |
18 |
Correct |
40 ms |
14544 KB |
Output is correct |
19 |
Correct |
31 ms |
14504 KB |
Output is correct |
20 |
Correct |
32 ms |
14540 KB |
Output is correct |
21 |
Correct |
25 ms |
14548 KB |
Output is correct |
22 |
Correct |
26 ms |
14420 KB |
Output is correct |
23 |
Correct |
25 ms |
14304 KB |
Output is correct |
24 |
Correct |
37 ms |
14200 KB |
Output is correct |
25 |
Correct |
28 ms |
14164 KB |
Output is correct |
26 |
Correct |
23 ms |
14476 KB |
Output is correct |
27 |
Correct |
22 ms |
14532 KB |
Output is correct |
28 |
Correct |
25 ms |
14516 KB |
Output is correct |
29 |
Correct |
24 ms |
14668 KB |
Output is correct |
30 |
Correct |
26 ms |
14480 KB |
Output is correct |
31 |
Correct |
27 ms |
14532 KB |
Output is correct |
32 |
Correct |
25 ms |
14520 KB |
Output is correct |
33 |
Correct |
28 ms |
14404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
14508 KB |
Output is correct |
2 |
Correct |
24 ms |
14484 KB |
Output is correct |
3 |
Correct |
25 ms |
14468 KB |
Output is correct |
4 |
Correct |
40 ms |
14544 KB |
Output is correct |
5 |
Correct |
31 ms |
14504 KB |
Output is correct |
6 |
Correct |
32 ms |
14540 KB |
Output is correct |
7 |
Correct |
25 ms |
14548 KB |
Output is correct |
8 |
Correct |
26 ms |
14420 KB |
Output is correct |
9 |
Correct |
25 ms |
14304 KB |
Output is correct |
10 |
Correct |
37 ms |
14200 KB |
Output is correct |
11 |
Correct |
28 ms |
14164 KB |
Output is correct |
12 |
Correct |
23 ms |
14476 KB |
Output is correct |
13 |
Correct |
22 ms |
14532 KB |
Output is correct |
14 |
Correct |
25 ms |
14516 KB |
Output is correct |
15 |
Correct |
24 ms |
14668 KB |
Output is correct |
16 |
Correct |
26 ms |
14480 KB |
Output is correct |
17 |
Correct |
27 ms |
14532 KB |
Output is correct |
18 |
Correct |
25 ms |
14520 KB |
Output is correct |
19 |
Correct |
28 ms |
14404 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 |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
149 ms |
88404 KB |
Output is correct |
35 |
Correct |
154 ms |
88556 KB |
Output is correct |
36 |
Correct |
222 ms |
88564 KB |
Output is correct |
37 |
Correct |
153 ms |
88396 KB |
Output is correct |
38 |
Correct |
170 ms |
88480 KB |
Output is correct |
39 |
Correct |
152 ms |
88524 KB |
Output is correct |
40 |
Correct |
153 ms |
88268 KB |
Output is correct |
41 |
Correct |
161 ms |
88340 KB |
Output is correct |
42 |
Correct |
143 ms |
88348 KB |
Output is correct |
43 |
Correct |
84 ms |
35768 KB |
Output is correct |
44 |
Correct |
113 ms |
53208 KB |
Output is correct |
45 |
Correct |
115 ms |
53224 KB |
Output is correct |
46 |
Correct |
119 ms |
53424 KB |
Output is correct |
47 |
Correct |
160 ms |
88536 KB |
Output is correct |
48 |
Correct |
168 ms |
88524 KB |
Output is correct |
49 |
Correct |
191 ms |
88936 KB |
Output is correct |
50 |
Correct |
161 ms |
89096 KB |
Output is correct |
51 |
Correct |
183 ms |
89048 KB |
Output is correct |
52 |
Correct |
153 ms |
90084 KB |
Output is correct |
53 |
Correct |
146 ms |
89560 KB |
Output is correct |
54 |
Correct |
164 ms |
88316 KB |
Output is correct |
55 |
Correct |
150 ms |
88560 KB |
Output is correct |
56 |
Correct |
150 ms |
88468 KB |
Output is correct |
57 |
Correct |
151 ms |
88556 KB |
Output is correct |
58 |
Correct |
148 ms |
88568 KB |
Output is correct |
59 |
Correct |
143 ms |
88500 KB |
Output is correct |
60 |
Correct |
140 ms |
88476 KB |
Output is correct |
61 |
Correct |
173 ms |
87372 KB |
Output is correct |
62 |
Correct |
151 ms |
88140 KB |
Output is correct |
63 |
Correct |
155 ms |
87628 KB |
Output is correct |