#include <bits/stdc++.h>
#include <peru.h>
using namespace std;
typedef long long ll;
const ll inf = 1e16;
const int MAXN = 2'500'000 + 25;
const int MOD = 1e9 + 7;
int add (int a, int b) {
a += b; if (a >= MOD) a -= MOD;
return a;
}
int sub (int a, int b) {
a -= b; if (a < 0) a += MOD;
return a;
}
int mul (int a, int b) {
return (a * 1ll * b) % MOD;
}
int pw[MAXN];
ll a[MAXN], n, k;
deque <array <ll, 4>> cur;
deque <ll> dd;
ll dp[MAXN];
struct MinimumStack {
stack <pair <ll, ll>> dd;
ll get () {
return dd.empty() ? inf : dd.top().second;
}
void pop() { dd.pop(); }
bool empty () { return dd.empty(); }
void push (ll x) {
ll mn = x; if (!empty()) x = min(x, get());
dd.push({x, mn});
}
int size () {
return (int)dd.size();
}
ll top () { return dd.empty() ? inf : dd.top().first; }
void swap (MinimumStack &x) {
dd.swap(x.dd);
}
};
struct MonotonicDeque {
deque <ll> cur;
void push_front (ll x) {
cur.push_front(x);
}
void push_back (ll x) {
cur.push_back(x);
}
void pop_front () {
cur.pop_front();
}
void pop_back () {
cur.pop_back();
}
ll get () {
ll ret = 1e18;
for (auto i : cur) ret = min(ret, i);
return ret;
}
// MinimumStack left, right, temp;
// void upd () {
// if (right.empty() && left.empty()) return;
// bool f = 0;
// if (right.empty()) {
// f = 1; left.swap(right);
// }
// int x = (int)right.size() / 2;
// while (x--) {
// temp.push(right.top());
// right.pop();
// }
// while (!right.empty()) {
// left.push(right.top()); right.pop();
// }
// while (!temp.empty()) {
// right.push(temp.top()); temp.pop();
// }
// if (f) {
// left.swap(right);
// }
// }
// ll get () {
// return min(left.get(), right.get());
// }
// int size () {
// return (int)left.size() + (int)right.size();
// }
// bool empty() {
// return size() == 0;
// }
// void push_back (ll x) {
// right.push(x);
// }
// void pop_back() {
// if (right.empty()) upd();
// right.pop();
// }
// void push_front (ll x) {
// left.push(x);
// }
// void pop_front() {
// if (left.empty()) upd();
// left.pop();
// }
// ll back () {
// if (right.empty()) upd();
// return right.top();
// }
// ll front () {
// if (left.empty()) upd();
// return left.top();
// }
} cur2;
int solve (int n, int k, int *a) {
pw[0] = 1; for (int i = 1; i < MAXN; i++) {
pw[i] = mul(23, pw[i - 1]);
}
int last = 0;
for (int i = 1; i <= n; i++) {
array <ll, 4> v = {i, i - 1, i - 1, dp[i - 1]};
while (!cur.empty() && a[cur.back()[0] - 1] < a[i - 1]) {
v[1] = cur.back()[1], v[3] = min(v[3], cur.back()[3]);
cur.pop_back();
cur2.pop_back();
}
cur.push_back(v); cur2.push_back(a[i - 1] + v[3]);
while (!cur.empty() && cur[0][0] <= i - k) {
cur.pop_front(); cur2.pop_front();
}
for (int j = last; j < cur[0][0]; j++) {
while (!dd.empty() && dp[dd.back()] >= dp[j]) dd.pop_back();
dd.push_back(j);
}
last = cur[0][0];
while (!dd.empty() && dd.front() < i - k) dd.pop_front();
cur2.pop_front();
cur2.push_front(a[cur[0][0] - 1] + dp[dd.front()]);
/*cout << i << ": \n";
for (auto j : cur) {
for (auto l : j) cout << l << " ";
cout << '\n';
}
cout << '\n';
for (auto j : dd) cout << j << " ";
cout << '\n' << '\n';
*/
//cout << cur[0][0] << " " << a[cur[0][0] - 1] << " " << dd.front() << " " << dp[dd.front()] << '\n';
//ll mx = a[cur[0][0] - 1] + dp[dd.front()];
//mx = min(mx, cur2.get());
//for(auto j:cur)if (j[0]!=last)mx=min(mx,a[j[0]-1]+j[3]);
/*cout << cur2.get() << '\n';
for (auto j : cur2.cur) cout << j << " ";
cout << '\n';*/
dp[i] = cur2.get();
}
// for (int i = 1; i <= n; i++) cout << dp[i] << " ";
//cout << '\n';
int ret = 0;
for (int i = 1; i <= n; i++) ret = add(ret, mul(dp[i] % MOD, pw[n - i]));
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14680 KB |
Output is correct |
2 |
Correct |
16 ms |
14684 KB |
Output is correct |
3 |
Correct |
15 ms |
14848 KB |
Output is correct |
4 |
Correct |
15 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14852 KB |
Output is correct |
6 |
Correct |
15 ms |
14812 KB |
Output is correct |
7 |
Correct |
14 ms |
14684 KB |
Output is correct |
8 |
Correct |
14 ms |
14808 KB |
Output is correct |
9 |
Correct |
15 ms |
14684 KB |
Output is correct |
10 |
Correct |
15 ms |
14856 KB |
Output is correct |
11 |
Correct |
15 ms |
14936 KB |
Output is correct |
12 |
Correct |
15 ms |
14684 KB |
Output is correct |
13 |
Correct |
15 ms |
14684 KB |
Output is correct |
14 |
Correct |
14 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14680 KB |
Output is correct |
2 |
Correct |
16 ms |
14684 KB |
Output is correct |
3 |
Correct |
15 ms |
14848 KB |
Output is correct |
4 |
Correct |
15 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14852 KB |
Output is correct |
6 |
Correct |
15 ms |
14812 KB |
Output is correct |
7 |
Correct |
14 ms |
14684 KB |
Output is correct |
8 |
Correct |
14 ms |
14808 KB |
Output is correct |
9 |
Correct |
15 ms |
14684 KB |
Output is correct |
10 |
Correct |
15 ms |
14856 KB |
Output is correct |
11 |
Correct |
15 ms |
14936 KB |
Output is correct |
12 |
Correct |
15 ms |
14684 KB |
Output is correct |
13 |
Correct |
15 ms |
14684 KB |
Output is correct |
14 |
Correct |
14 ms |
14684 KB |
Output is correct |
15 |
Correct |
112 ms |
23364 KB |
Output is correct |
16 |
Correct |
114 ms |
23380 KB |
Output is correct |
17 |
Correct |
123 ms |
23564 KB |
Output is correct |
18 |
Correct |
38 ms |
23540 KB |
Output is correct |
19 |
Correct |
38 ms |
23516 KB |
Output is correct |
20 |
Correct |
37 ms |
23520 KB |
Output is correct |
21 |
Correct |
160 ms |
23580 KB |
Output is correct |
22 |
Correct |
363 ms |
23628 KB |
Output is correct |
23 |
Correct |
412 ms |
23640 KB |
Output is correct |
24 |
Correct |
259 ms |
23576 KB |
Output is correct |
25 |
Correct |
247 ms |
23708 KB |
Output is correct |
26 |
Correct |
100 ms |
23436 KB |
Output is correct |
27 |
Correct |
117 ms |
23380 KB |
Output is correct |
28 |
Correct |
136 ms |
23616 KB |
Output is correct |
29 |
Correct |
162 ms |
23580 KB |
Output is correct |
30 |
Correct |
153 ms |
23380 KB |
Output is correct |
31 |
Correct |
106 ms |
23452 KB |
Output is correct |
32 |
Correct |
200 ms |
23424 KB |
Output is correct |
33 |
Correct |
103 ms |
23480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
23364 KB |
Output is correct |
2 |
Correct |
114 ms |
23380 KB |
Output is correct |
3 |
Correct |
123 ms |
23564 KB |
Output is correct |
4 |
Correct |
38 ms |
23540 KB |
Output is correct |
5 |
Correct |
38 ms |
23516 KB |
Output is correct |
6 |
Correct |
37 ms |
23520 KB |
Output is correct |
7 |
Correct |
160 ms |
23580 KB |
Output is correct |
8 |
Correct |
363 ms |
23628 KB |
Output is correct |
9 |
Correct |
412 ms |
23640 KB |
Output is correct |
10 |
Correct |
259 ms |
23576 KB |
Output is correct |
11 |
Correct |
247 ms |
23708 KB |
Output is correct |
12 |
Correct |
100 ms |
23436 KB |
Output is correct |
13 |
Correct |
117 ms |
23380 KB |
Output is correct |
14 |
Correct |
136 ms |
23616 KB |
Output is correct |
15 |
Correct |
162 ms |
23580 KB |
Output is correct |
16 |
Correct |
153 ms |
23380 KB |
Output is correct |
17 |
Correct |
106 ms |
23452 KB |
Output is correct |
18 |
Correct |
200 ms |
23424 KB |
Output is correct |
19 |
Correct |
103 ms |
23480 KB |
Output is correct |
20 |
Correct |
15 ms |
14680 KB |
Output is correct |
21 |
Correct |
16 ms |
14684 KB |
Output is correct |
22 |
Correct |
15 ms |
14848 KB |
Output is correct |
23 |
Correct |
15 ms |
14684 KB |
Output is correct |
24 |
Correct |
14 ms |
14852 KB |
Output is correct |
25 |
Correct |
15 ms |
14812 KB |
Output is correct |
26 |
Correct |
14 ms |
14684 KB |
Output is correct |
27 |
Correct |
14 ms |
14808 KB |
Output is correct |
28 |
Correct |
15 ms |
14684 KB |
Output is correct |
29 |
Correct |
15 ms |
14856 KB |
Output is correct |
30 |
Correct |
15 ms |
14936 KB |
Output is correct |
31 |
Correct |
15 ms |
14684 KB |
Output is correct |
32 |
Correct |
15 ms |
14684 KB |
Output is correct |
33 |
Correct |
14 ms |
14684 KB |
Output is correct |
34 |
Execution timed out |
1014 ms |
42632 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |