#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, 2>> 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;
}
} 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, 2> v = {i, dp[i - 1]};
while (!cur.empty() && a[cur.back()[0] - 1] <= a[i - 1]) {
v[1] = min(v[1], cur.back()[1]);
cur.pop_back();
cur2.pop_back();
}
cur.push_back(v); cur2.push_back(a[i - 1] + v[1]);
cout << cur2.get() << '\n';
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()]);
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;
}
/*
int main () {
int n, k; cin >> n >> k;
int a[n + 1] = {};
for (int i = 0; i < n; i++) cin >> a[i];
cout << solve(n, k, a) << '\n';
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
14680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
14680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
14680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |