#include "peru.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #pragma GCC optimize("O3,unroll-loops,no-stack-protector")
// #pragma GCC target("mmx,sse,sse2,sse3,ssse3,sse4,avx,avx2,fma")
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 2e6 + 5e5 + 5, M = 22, mod = 1e9 + 7;
const ll infll = (ld)1e18 + 7;
namespace {
deque <pair <int, ll>> dq;
multiset <ll> mtsdp;
ll dp[N];
int sparse[M][N];
deque <int> dq2;
int convert(int n){
int ans = 0;
For(i, 0, n){
ans = (ll)ans * 23 % mod;
ans += dp[i] % mod;
if (ans >= mod){
ans -= mod;
}
}
return ans;
}
}
int solve(int n, int k, int* a){
For(i, 0, n){
sparse[0][i] = a[i];
}
For(j, 1, M){
ForE(i, 0, n - (1 << j)){
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
if (k == 1){
For(i, 0, n){
dp[i] = dp[i - 1] + a[i];
}
return convert(n);
}
For(i, 0, n){
int val = 0;
while (!dq.empty() and a[dq.front().fi] <= a[i]){
mtsdp.erase(mtsdp.find(dq.front().se));
val = a[dq.front().fi];
dq.pop_front();
}
if (!dq.empty()){
mtsdp.erase(mtsdp.find(dq.front().se));
dq.front().se -= val;
dq.front().se += a[i];
mtsdp.insert(dq.front().se);
}
while (!dq2.empty() and a[dq2.front()] <= a[i]){
dq2.pop_front();
}
dq2.push_front(i);
dp[i] = infll;
if (!mtsdp.empty()){
dp[i] = *mtsdp.begin();
}
dp[i] = min(dp[i], a[dq2.back()] + (i >= k ? dp[i - k] : 0));
dq.push_front(make_pair(i, dp[i]));
mtsdp.insert(dp[i]);
if (!dq.empty() and dq.back().fi == i - k + 1){
mtsdp.erase(mtsdp.find(dq.back().se));
dq.pop_back();
}
if (!dq2.empty() and dq2.back() == i - k + 1){
dq2.pop_back();
}
}
// For(i, 0, n){
// cout << dp[i] << ' ';
// } cout << endl;
return convert(n);
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
119 ms |
33468 KB |
Output is correct |
16 |
Correct |
117 ms |
34144 KB |
Output is correct |
17 |
Correct |
115 ms |
34152 KB |
Output is correct |
18 |
Correct |
84 ms |
34088 KB |
Output is correct |
19 |
Correct |
82 ms |
34124 KB |
Output is correct |
20 |
Correct |
101 ms |
34116 KB |
Output is correct |
21 |
Correct |
119 ms |
34196 KB |
Output is correct |
22 |
Correct |
141 ms |
34260 KB |
Output is correct |
23 |
Correct |
143 ms |
34284 KB |
Output is correct |
24 |
Correct |
144 ms |
34192 KB |
Output is correct |
25 |
Correct |
134 ms |
34124 KB |
Output is correct |
26 |
Correct |
112 ms |
34128 KB |
Output is correct |
27 |
Correct |
111 ms |
34296 KB |
Output is correct |
28 |
Correct |
131 ms |
34152 KB |
Output is correct |
29 |
Correct |
123 ms |
34436 KB |
Output is correct |
30 |
Correct |
130 ms |
34172 KB |
Output is correct |
31 |
Correct |
121 ms |
34100 KB |
Output is correct |
32 |
Correct |
118 ms |
34260 KB |
Output is correct |
33 |
Correct |
124 ms |
34128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
33468 KB |
Output is correct |
2 |
Correct |
117 ms |
34144 KB |
Output is correct |
3 |
Correct |
115 ms |
34152 KB |
Output is correct |
4 |
Correct |
84 ms |
34088 KB |
Output is correct |
5 |
Correct |
82 ms |
34124 KB |
Output is correct |
6 |
Correct |
101 ms |
34116 KB |
Output is correct |
7 |
Correct |
119 ms |
34196 KB |
Output is correct |
8 |
Correct |
141 ms |
34260 KB |
Output is correct |
9 |
Correct |
143 ms |
34284 KB |
Output is correct |
10 |
Correct |
144 ms |
34192 KB |
Output is correct |
11 |
Correct |
134 ms |
34124 KB |
Output is correct |
12 |
Correct |
112 ms |
34128 KB |
Output is correct |
13 |
Correct |
111 ms |
34296 KB |
Output is correct |
14 |
Correct |
131 ms |
34152 KB |
Output is correct |
15 |
Correct |
123 ms |
34436 KB |
Output is correct |
16 |
Correct |
130 ms |
34172 KB |
Output is correct |
17 |
Correct |
121 ms |
34100 KB |
Output is correct |
18 |
Correct |
118 ms |
34260 KB |
Output is correct |
19 |
Correct |
124 ms |
34128 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Execution timed out |
787 ms |
229428 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |