#include <bits/stdc++.h>
#include "peru.h"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vpll vector<pll>
#define matrix vector<vi>
#define matrixLL vector<vll>
#define vs vector<string>
#define vui vector<ui>
#define msi multiset<int>
#define mss multiset<string>
#define si set<int>
#define ss set<string>
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int dxx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dyy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const string abc="abcdefghijklmnopqrstuvwxyz";
const string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ld pi = 3.14159265;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 3e6 + 7;
const int inf = mod;
const ll INF = 1e18;
const ll zero = ll(0);
const int logo = 20;
const int off = 1 << logo;
const int trsz = off << 1;
ll dp[MAXN];
stack<ll> l, r;
struct node{
ll opt;
int arr, ind;
node(ll opt, int arr, int ind){
this->opt = opt;
this->arr = arr;
this->ind = ind;
}
};
deque<node> d;
int add(ll a, ll b){
return (a + b) % mod;
}
int mul(ll a, ll b){
return (a * b) % mod;
}
ll get_min(){
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 rebuild(){
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
int sz = d.size();
int line = sz / 2;
vector<node> v;
REP(i, line) v.PB(d.front()), d.PPF();
while(!v.empty()){
push(l, v.back().opt);
d.PF(v.back());
v.PPB();
}
REP(i, sz - line) v.PB(d.back()), d.PPB();
while(!v.empty()){
push(r, v.back().opt);
d.PB(v.back());
v.PPB();
}
}
int solve(int n, int k, int *arr){
int ret = 0;
for(int i = 1; i <= n; i++){
if(i == 1){
dp[i] = arr[i - 1];
d.PB(node(dp[i], arr[i - 1], i));
push(r, arr[i - 1]);
ret = add(mul(ret, 23), dp[i]);
continue;
}
//prvi veci u stacku(1 indeks.)
int dpind = i;
while(!d.empty() and d.back().arr <= arr[i - 1]){
dpind = d.back().ind;
d.PPB();
if(r.empty()) rebuild();
else r.pop();
}
d.PB(node(dp[dpind - 1] + arr[i - 1], arr[i - 1], dpind));
push(r, dp[dpind - 1] + arr[i - 1]);
if(i > k and d.front().ind <= i - k){
ll arrmax = d.front().arr;
d.pop_front();
if(l.empty()) rebuild();
else l.pop();
//ak sam popo maksimum i maks je zadni ubacen
if(d.empty()){
d.push_front(node(dp[i - k] + arrmax, arrmax, i - k + 1));
push(l, dp[i - k] + arrmax);
}else if(d.front().ind > i - k + 1){
//ako onaj kojeg sam popo nije najlijeviji ga vrati nazad
d.push_front(node(dp[i - k] + arrmax, arrmax, i - k + 1));
push(l, dp[i - k] + arrmax);
}
}
dp[i] = get_min();
//cout << dp[i] << " ";
ret = add(mul(ret, 23), dp[i]);
}
return ret;
}
/*
void solv(){
int n, k;
cin >> n >> k;
vi arr; arr.clear();
REP(i, n){
int x;
cin >> x;
arr.PB(x);
}
cout << solve(n, k, arr);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin >> t;
while(t--)solv();
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
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 |
332 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 |
336 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 |
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 |
332 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 |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
26 ms |
9148 KB |
Output is correct |
16 |
Correct |
24 ms |
9180 KB |
Output is correct |
17 |
Correct |
26 ms |
9204 KB |
Output is correct |
18 |
Correct |
26 ms |
9204 KB |
Output is correct |
19 |
Correct |
29 ms |
9140 KB |
Output is correct |
20 |
Correct |
24 ms |
9144 KB |
Output is correct |
21 |
Correct |
24 ms |
9124 KB |
Output is correct |
22 |
Correct |
25 ms |
9164 KB |
Output is correct |
23 |
Correct |
25 ms |
9308 KB |
Output is correct |
24 |
Correct |
27 ms |
9204 KB |
Output is correct |
25 |
Correct |
27 ms |
9136 KB |
Output is correct |
26 |
Correct |
26 ms |
9128 KB |
Output is correct |
27 |
Correct |
28 ms |
9220 KB |
Output is correct |
28 |
Correct |
23 ms |
9248 KB |
Output is correct |
29 |
Correct |
23 ms |
9380 KB |
Output is correct |
30 |
Correct |
24 ms |
9212 KB |
Output is correct |
31 |
Correct |
28 ms |
9132 KB |
Output is correct |
32 |
Correct |
25 ms |
9232 KB |
Output is correct |
33 |
Correct |
30 ms |
9188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
9148 KB |
Output is correct |
2 |
Correct |
24 ms |
9180 KB |
Output is correct |
3 |
Correct |
26 ms |
9204 KB |
Output is correct |
4 |
Correct |
26 ms |
9204 KB |
Output is correct |
5 |
Correct |
29 ms |
9140 KB |
Output is correct |
6 |
Correct |
24 ms |
9144 KB |
Output is correct |
7 |
Correct |
24 ms |
9124 KB |
Output is correct |
8 |
Correct |
25 ms |
9164 KB |
Output is correct |
9 |
Correct |
25 ms |
9308 KB |
Output is correct |
10 |
Correct |
27 ms |
9204 KB |
Output is correct |
11 |
Correct |
27 ms |
9136 KB |
Output is correct |
12 |
Correct |
26 ms |
9128 KB |
Output is correct |
13 |
Correct |
28 ms |
9220 KB |
Output is correct |
14 |
Correct |
23 ms |
9248 KB |
Output is correct |
15 |
Correct |
23 ms |
9380 KB |
Output is correct |
16 |
Correct |
24 ms |
9212 KB |
Output is correct |
17 |
Correct |
28 ms |
9132 KB |
Output is correct |
18 |
Correct |
25 ms |
9232 KB |
Output is correct |
19 |
Correct |
30 ms |
9188 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 |
332 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 |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
141 ms |
32880 KB |
Output is correct |
35 |
Correct |
138 ms |
53532 KB |
Output is correct |
36 |
Correct |
156 ms |
53540 KB |
Output is correct |
37 |
Correct |
144 ms |
53508 KB |
Output is correct |
38 |
Correct |
147 ms |
53552 KB |
Output is correct |
39 |
Correct |
138 ms |
53504 KB |
Output is correct |
40 |
Correct |
160 ms |
53460 KB |
Output is correct |
41 |
Correct |
151 ms |
53456 KB |
Output is correct |
42 |
Correct |
152 ms |
53560 KB |
Output is correct |
43 |
Correct |
58 ms |
21824 KB |
Output is correct |
44 |
Correct |
92 ms |
33432 KB |
Output is correct |
45 |
Correct |
87 ms |
33320 KB |
Output is correct |
46 |
Correct |
108 ms |
33244 KB |
Output is correct |
47 |
Correct |
138 ms |
55612 KB |
Output is correct |
48 |
Correct |
143 ms |
55780 KB |
Output is correct |
49 |
Correct |
140 ms |
55908 KB |
Output is correct |
50 |
Correct |
129 ms |
56628 KB |
Output is correct |
51 |
Correct |
145 ms |
56240 KB |
Output is correct |
52 |
Correct |
137 ms |
56840 KB |
Output is correct |
53 |
Correct |
139 ms |
56516 KB |
Output is correct |
54 |
Correct |
150 ms |
53520 KB |
Output is correct |
55 |
Correct |
132 ms |
53384 KB |
Output is correct |
56 |
Correct |
130 ms |
53324 KB |
Output is correct |
57 |
Correct |
148 ms |
53396 KB |
Output is correct |
58 |
Correct |
134 ms |
53412 KB |
Output is correct |
59 |
Correct |
155 ms |
53516 KB |
Output is correct |
60 |
Correct |
135 ms |
53532 KB |
Output is correct |
61 |
Correct |
155 ms |
55740 KB |
Output is correct |
62 |
Correct |
153 ms |
55848 KB |
Output is correct |
63 |
Correct |
159 ms |
55612 KB |
Output is correct |