# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1089527 |
2024-09-16T15:52:26 Z |
LePhiPhat |
Peru (RMI20_peru) |
C++17 |
|
356 ms |
98316 KB |
// @author: Le Phi Phat
#include "peru.h"
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.cpp"
#else
#define debug(...) 42
#define debugArr(...) 42
#endif
using namespace std;
template <class X, class Y> bool mini(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <class X, class Y> bool maxi(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
using i64 = long long;
using vi = vector<int>;
template <class T> class minStack {
public:
stack<pair<T, T>> st;
T getMin() { return st.top().second; }
T empty() { return st.empty(); }
int size() { return st.size(); }
void push(T x) {
T mn = x;
if (!empty()) {
mini(mn, getMin());
}
st.push(make_pair(x, mn));
}
void pop() { st.pop(); }
T top() { return st.top().first; }
void swap(minStack<T> &another) { st.swap(another.st); }
};
template <class T> class minDeque {
public:
minStack<T> stackF, stackB, stackTmp;
void rebalance() {
bool flag = false;
if (stackF.empty()) {
stackF.swap(stackB);
flag = true;
}
int sz = stackF.size() / 2;
while (sz--) {
stackTmp.push(stackF.top());
stackF.pop();
}
while (!stackF.empty()) {
stackB.push(stackF.top());
stackF.pop();
}
while (!stackTmp.empty()) {
stackF.push(stackTmp.top());
stackTmp.pop();
}
if (flag) {
stackF.swap(stackB);
}
}
bool empty() { return (stackF.empty() && stackB.empty()); }
int size() { return (stackB.size() + stackF.size()); }
void push_back(T x) { stackB.push(x); }
void push_front(T x) { stackF.push(x); }
void pop_back() {
assert(stackB.size() || stackF.size());
if (stackB.empty())
rebalance();
stackB.pop();
}
void pop_front() {
assert(stackB.size() || stackF.size());
if (stackF.empty())
rebalance();
stackF.pop();
}
T back() {
if (stackB.empty())
rebalance();
return stackB.top();
}
T front() {
if (stackF.empty())
rebalance();
return stackF.top();
}
T getMin() {
assert(stackB.size() || stackF.size());
if (stackF.empty())
return stackB.getMin();
if (stackB.empty())
return stackF.getMin();
return min(stackF.getMin(), stackB.getMin());
}
};
void debugNl(minDeque<int> de) {
cout << "[ ";
while (de.stackF.size()) {
cout << de.stackF.top() << ", ";
de.stackF.pop();
}
vector<int> v;
while (de.stackB.size()) {
v.push_back(de.stackB.top());
de.stackB.pop();
}
while (not v.empty()) {
cout << v.back() << ", ";
v.pop_back();
}
cout << "]\n";
}
const i64 MOD = 1'000'000'007;
i64 mul(i64 a, i64 b) { return (a % MOD) * (b % MOD) % MOD; }
int solve(int N, int K, int *S) {
vector<i64> dp(N + 1, 1e16), save(N + 1, 1e16);
vector<i64> a(N + 1);
for (int i = 1; i <= N; i++) {
a[i] = S[i - 1];
}
deque<int> de, val;
multiset<i64> ms;
dp[0] = 0;
de.push_back(0);
for (int i = 1; i <= N; i++) {
while (!de.empty() && a[de.back()] <= a[i]) {
int p = de.back();
de.pop_back();
if (!de.empty()) {
ms.erase(ms.find(dp[de.back()] + a[p]));
}
}
while (!de.empty() && i - de.front() >= K) {
int p = de.front();
de.pop_front();
if (!de.empty()) {
ms.erase(ms.find(dp[p] + a[de.front()]));
}
}
if (!de.empty()) {
ms.insert(dp[de.back()] + a[i]);
}
de.push_back(i);
mini(dp[i], dp[max(0, i - K)] + a[de.front()]);
if (!ms.empty()) {
mini(dp[i], *(ms.begin()));
}
}
// for (int i = 1; i <= N; i++) {
// cout << dp[i] << " \n"[i == N];
// }
i64 cur = 1, ans = 0;
for (int i = N; i >= 1; i--) {
ans = (ans % MOD + mul(cur, dp[i])) % MOD;
cur = mul(cur, 23);
}
return ans;
}
// #ifdef LOCAL
// signed main() {
// ios::sync_with_stdio(0);
// cout.tie(0);
// cin.tie(0);
// #ifdef LOCAL
// freopen("in", "r", stdin);
// #endif
// int N = 8, K = 2;
// cin >> N >> K;
// int a[N];
// for (int i = 0; i < N; i++) {
// cin >> a[i];
// }
// cout << solve(N, K, a);
// }
// #endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
49 ms |
15660 KB |
Output is correct |
16 |
Correct |
50 ms |
15668 KB |
Output is correct |
17 |
Correct |
52 ms |
15488 KB |
Output is correct |
18 |
Correct |
46 ms |
15496 KB |
Output is correct |
19 |
Correct |
48 ms |
15628 KB |
Output is correct |
20 |
Correct |
37 ms |
15444 KB |
Output is correct |
21 |
Correct |
51 ms |
15596 KB |
Output is correct |
22 |
Correct |
54 ms |
15620 KB |
Output is correct |
23 |
Correct |
50 ms |
15700 KB |
Output is correct |
24 |
Correct |
52 ms |
15664 KB |
Output is correct |
25 |
Correct |
52 ms |
15668 KB |
Output is correct |
26 |
Correct |
48 ms |
15452 KB |
Output is correct |
27 |
Correct |
51 ms |
15448 KB |
Output is correct |
28 |
Correct |
48 ms |
15700 KB |
Output is correct |
29 |
Correct |
46 ms |
15872 KB |
Output is correct |
30 |
Correct |
49 ms |
15452 KB |
Output is correct |
31 |
Correct |
48 ms |
15644 KB |
Output is correct |
32 |
Correct |
49 ms |
15708 KB |
Output is correct |
33 |
Correct |
48 ms |
15396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15660 KB |
Output is correct |
2 |
Correct |
50 ms |
15668 KB |
Output is correct |
3 |
Correct |
52 ms |
15488 KB |
Output is correct |
4 |
Correct |
46 ms |
15496 KB |
Output is correct |
5 |
Correct |
48 ms |
15628 KB |
Output is correct |
6 |
Correct |
37 ms |
15444 KB |
Output is correct |
7 |
Correct |
51 ms |
15596 KB |
Output is correct |
8 |
Correct |
54 ms |
15620 KB |
Output is correct |
9 |
Correct |
50 ms |
15700 KB |
Output is correct |
10 |
Correct |
52 ms |
15664 KB |
Output is correct |
11 |
Correct |
52 ms |
15668 KB |
Output is correct |
12 |
Correct |
48 ms |
15452 KB |
Output is correct |
13 |
Correct |
51 ms |
15448 KB |
Output is correct |
14 |
Correct |
48 ms |
15700 KB |
Output is correct |
15 |
Correct |
46 ms |
15872 KB |
Output is correct |
16 |
Correct |
49 ms |
15452 KB |
Output is correct |
17 |
Correct |
48 ms |
15644 KB |
Output is correct |
18 |
Correct |
49 ms |
15708 KB |
Output is correct |
19 |
Correct |
48 ms |
15396 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
318 ms |
92704 KB |
Output is correct |
35 |
Correct |
300 ms |
92896 KB |
Output is correct |
36 |
Correct |
299 ms |
92776 KB |
Output is correct |
37 |
Correct |
332 ms |
93028 KB |
Output is correct |
38 |
Correct |
356 ms |
92884 KB |
Output is correct |
39 |
Correct |
341 ms |
92912 KB |
Output is correct |
40 |
Correct |
325 ms |
92752 KB |
Output is correct |
41 |
Correct |
323 ms |
92660 KB |
Output is correct |
42 |
Correct |
317 ms |
93012 KB |
Output is correct |
43 |
Correct |
122 ms |
37708 KB |
Output is correct |
44 |
Correct |
125 ms |
56900 KB |
Output is correct |
45 |
Correct |
126 ms |
56912 KB |
Output is correct |
46 |
Correct |
127 ms |
57248 KB |
Output is correct |
47 |
Correct |
302 ms |
95056 KB |
Output is correct |
48 |
Correct |
314 ms |
95216 KB |
Output is correct |
49 |
Correct |
315 ms |
95572 KB |
Output is correct |
50 |
Correct |
321 ms |
96576 KB |
Output is correct |
51 |
Correct |
314 ms |
96136 KB |
Output is correct |
52 |
Correct |
329 ms |
98316 KB |
Output is correct |
53 |
Correct |
312 ms |
97040 KB |
Output is correct |
54 |
Correct |
310 ms |
92700 KB |
Output is correct |
55 |
Correct |
290 ms |
92640 KB |
Output is correct |
56 |
Correct |
282 ms |
92652 KB |
Output is correct |
57 |
Correct |
294 ms |
92764 KB |
Output is correct |
58 |
Correct |
292 ms |
92616 KB |
Output is correct |
59 |
Correct |
301 ms |
92844 KB |
Output is correct |
60 |
Correct |
301 ms |
92740 KB |
Output is correct |
61 |
Correct |
340 ms |
95152 KB |
Output is correct |
62 |
Correct |
328 ms |
95116 KB |
Output is correct |
63 |
Correct |
339 ms |
94872 KB |
Output is correct |