#include "peru.h"
#include <set>
#include <deque>
#define magic_sauce inline __attribute__((always_inline))
using ll = long long;
const ll INF = 1e18;
const int MOD = 1e9 + 7;
magic_sauce ll min( ll a, ll b ){ return a < b ? a : b; }
magic_sauce ll max( ll a, ll b ){ return a > b ? a : b; }
int ans_hash( int n, ll v[] ){
int ret = 0;
for( int i = 0 ; i < n ; i++ )
ret = (ret * 23LL + v[i]) % MOD;
return ret;
}
/*
ans[i] = min{ ans[max{ i - k, prev_bigger( x ) }] + x | x=v[i]..+INF }
// echivalent:
ans[i] = min{ ans[j] + max{ v[j+1..i] } | j=i-k..i-1 }
// folosim stiva de maxime partiale
// ans este crescator => daca avem un interval [st, dr] de maxime partiale egale atunci clar alegem pe st
// daca x[0..k-1] sunt pozitiile din stiva
// atunci are sens sa luam doar o pozitie de tipul x[i] + 1
// obs: santinela x[-1] = i-k
// tinem pozitiile de acest tip intr-un multiset
*/
int solve( int n, int k, int *v ){
ll *ans = new ll[n];
std::multiset<ll> samples;
std::deque<int> stack;
for( int i = 0 ; i < k ; i++ ){
while( !stack.empty() && v[stack.back()] < v[i] )
stack.pop_back();
stack.push_back( i );
ans[i] = v[stack.front()];
}
// sample stack succesors
for( int i = 0 ; i + 1 < (int)stack.size() ; i++ )
samples.insert( ans[stack[i]] + v[stack[i + 1]] );
samples.insert( +INF ); // santinela
for( int i = k ; i < n ; i++ ){
// adaugam v[i], scoatem v[i - k]
// push
while( !stack.empty() && v[stack.back()] < v[i] ){
int popped = stack.back();
stack.pop_back();
if( !stack.empty() )
samples.erase( samples.find( ans[stack.back()] + v[popped] ) );
}
if( !stack.empty() )
samples.insert( ans[stack.back()] + v[i] );
stack.push_back( i );
// pop
int j = i - k; // pentru convenienta
if( stack.front() == j ){
stack.pop_front();
if( !stack.empty() )
samples.erase( samples.find( ans[j] + v[stack.front()] ) );
}
ans[i] = min( ans[i - k] + v[stack.front()], *samples.begin() );
}
ll ret = ans_hash( n, ans );
delete []ans;
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
49 ms |
6916 KB |
Output is correct |
16 |
Correct |
44 ms |
6860 KB |
Output is correct |
17 |
Correct |
44 ms |
6740 KB |
Output is correct |
18 |
Correct |
32 ms |
6736 KB |
Output is correct |
19 |
Correct |
33 ms |
6744 KB |
Output is correct |
20 |
Correct |
34 ms |
10836 KB |
Output is correct |
21 |
Correct |
47 ms |
10832 KB |
Output is correct |
22 |
Correct |
50 ms |
11044 KB |
Output is correct |
23 |
Correct |
46 ms |
10892 KB |
Output is correct |
24 |
Correct |
48 ms |
10856 KB |
Output is correct |
25 |
Correct |
51 ms |
10836 KB |
Output is correct |
26 |
Correct |
46 ms |
10780 KB |
Output is correct |
27 |
Correct |
50 ms |
10944 KB |
Output is correct |
28 |
Correct |
46 ms |
11044 KB |
Output is correct |
29 |
Correct |
47 ms |
10980 KB |
Output is correct |
30 |
Correct |
46 ms |
11016 KB |
Output is correct |
31 |
Correct |
46 ms |
10772 KB |
Output is correct |
32 |
Correct |
47 ms |
11072 KB |
Output is correct |
33 |
Correct |
46 ms |
10916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
6916 KB |
Output is correct |
2 |
Correct |
44 ms |
6860 KB |
Output is correct |
3 |
Correct |
44 ms |
6740 KB |
Output is correct |
4 |
Correct |
32 ms |
6736 KB |
Output is correct |
5 |
Correct |
33 ms |
6744 KB |
Output is correct |
6 |
Correct |
34 ms |
10836 KB |
Output is correct |
7 |
Correct |
47 ms |
10832 KB |
Output is correct |
8 |
Correct |
50 ms |
11044 KB |
Output is correct |
9 |
Correct |
46 ms |
10892 KB |
Output is correct |
10 |
Correct |
48 ms |
10856 KB |
Output is correct |
11 |
Correct |
51 ms |
10836 KB |
Output is correct |
12 |
Correct |
46 ms |
10780 KB |
Output is correct |
13 |
Correct |
50 ms |
10944 KB |
Output is correct |
14 |
Correct |
46 ms |
11044 KB |
Output is correct |
15 |
Correct |
47 ms |
10980 KB |
Output is correct |
16 |
Correct |
46 ms |
11016 KB |
Output is correct |
17 |
Correct |
46 ms |
10772 KB |
Output is correct |
18 |
Correct |
47 ms |
11072 KB |
Output is correct |
19 |
Correct |
46 ms |
10916 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
288 ms |
52472 KB |
Output is correct |
35 |
Correct |
288 ms |
52644 KB |
Output is correct |
36 |
Correct |
306 ms |
52484 KB |
Output is correct |
37 |
Correct |
295 ms |
52656 KB |
Output is correct |
38 |
Correct |
298 ms |
52680 KB |
Output is correct |
39 |
Correct |
305 ms |
52620 KB |
Output is correct |
40 |
Correct |
301 ms |
52816 KB |
Output is correct |
41 |
Correct |
299 ms |
52516 KB |
Output is correct |
42 |
Correct |
304 ms |
52776 KB |
Output is correct |
43 |
Correct |
111 ms |
22920 KB |
Output is correct |
44 |
Correct |
125 ms |
34668 KB |
Output is correct |
45 |
Correct |
124 ms |
34364 KB |
Output is correct |
46 |
Correct |
126 ms |
34324 KB |
Output is correct |
47 |
Correct |
299 ms |
54600 KB |
Output is correct |
48 |
Correct |
297 ms |
54804 KB |
Output is correct |
49 |
Correct |
312 ms |
55100 KB |
Output is correct |
50 |
Correct |
281 ms |
56132 KB |
Output is correct |
51 |
Correct |
297 ms |
55952 KB |
Output is correct |
52 |
Correct |
317 ms |
57884 KB |
Output is correct |
53 |
Correct |
294 ms |
56372 KB |
Output is correct |
54 |
Correct |
297 ms |
52444 KB |
Output is correct |
55 |
Correct |
292 ms |
52520 KB |
Output is correct |
56 |
Correct |
270 ms |
52328 KB |
Output is correct |
57 |
Correct |
273 ms |
52456 KB |
Output is correct |
58 |
Correct |
283 ms |
52732 KB |
Output is correct |
59 |
Correct |
294 ms |
52812 KB |
Output is correct |
60 |
Correct |
285 ms |
52560 KB |
Output is correct |
61 |
Correct |
319 ms |
54748 KB |
Output is correct |
62 |
Correct |
309 ms |
54612 KB |
Output is correct |
63 |
Correct |
322 ms |
54596 KB |
Output is correct |