#include "peru.h"
#include <utility>
#include <cassert>
#include <tuple>
#include <utility>
#include <iostream>
#include <algorithm>
#include <stack>
#if 0
struct minstack {
stack<pair<int, int>> st;
int getmin() {return st.top().second;}
bool empty() {return st.empty();}
int size() {return st.size();}
void push(int x) {
int mn = x;
if (!empty()) mn = min(mn, getmin());
st.push({x, mn});
}
void pop() {st.pop();}
int top() {return st.top().first;}
void swap(minstack &x) {st.swap(x.st);}
};
struct mindeque {
minstack l, r, t;
void rebalance() {
bool f = false;
if (r.empty()) {f = true; l.swap(r);}
int sz = r.size() / 2;
while (sz--) {t.push(r.top()); r.pop();}
while (!r.empty()) {l.push(r.top()); r.pop();}
while (!t.empty()) {r.push(t.top()); t.pop();}
if (f) l.swap(r);
}
int getmin() {
if (l.empty()) return r.getmin();
if (r.empty()) return l.getmin();
return min(l.getmin(), r.getmin());
}
bool empty() {return l.empty() && r.empty();}
int size() {return l.size() + r.size();}
void push_front(int x) {l.push(x);}
void push_back(int x) {r.push(x);}
void pop_front() {if (l.empty()) rebalance(); l.pop();}
void pop_back() {if (r.empty()) rebalance(); r.pop();}
int front() {if (l.empty()) rebalance(); return l.top();}
int back() {if (r.empty()) rebalance(); return r.top();}
void swap(mindeque &x) {l.swap(x.l); r.swap(x.r);}
};
#endif
#if 1
template <typename T, const T& (*F)(const T&, const T&)>
struct stack
{
std::stack<std::pair<T, T>> a;
void push(const T &v)
{
if (a.empty()) a.push({v, v});
else a.push({v, F(a.top().second, v)});
}
bool empty()
{
return a.empty();
}
size_t size()
{
return a.size();
}
T top()
{
return a.top().first;
}
T op()
{
return a.top().second;
}
void pop()
{
a.pop();
}
void swap(stack &x) {a.swap(x.a);}
};
template <typename T, const T& (*F)(const T&, const T&)>
struct deque
{
stack<T, F> f, b, t;
deque() { }
void rebalance() {
bool a = false;
if (b.empty()) {a = true; f.swap(b);}
int sz = b.size() / 2;
while (sz--) {t.push(b.top()); b.pop();}
while (!b.empty()) {f.push(b.top()); b.pop();}
while (!t.empty()) {b.push(t.top()); t.pop();}
if (a) f.swap(b);
}
void push_back(const T &v)
{
b.push(v);
}
void pop_front()
{
if (f.empty()) rebalance();
f.pop();
}
void pop_back()
{
if (b.empty()) rebalance();
b.pop();
}
T front()
{
if (f.empty()) rebalance();
return f.top();
}
T back()
{
if (b.empty()) rebalance();
return b.top();
}
T op()
{
if (f.empty()) return b.op();
if (b.empty()) return f.op();
return F(f.op(), b.op());
}
size_t size()
{
return f.size() + b.size();
}
};
#else
template <typename T, const T& (*F)(const T&, const T&)>
struct deque :std::deque<T>
{
T op()
{
T x = this->front();
for (auto y : *this) x = F(x, y);
return x;
}
};
#endif
using i64 = long long;
using u64 = unsigned long long;
int solve(int n, int k, int* v)
{
i64 *dp = new i64[n];
std::deque<int> dq;
deque<i64, std::min> qry;
for (int i = 0; i < n; ++i)
{
while (dq.size() && v[dq.back()] <= v[i])
{
dq.pop_back();
if (dq.size()) qry.pop_back();
}
if (dq.size()) qry.push_back(dp[dq.back()] + v[i]);
dq.push_back(i);
if (dq.size() && dq.front() + k == i)
{
dq.pop_front();
if (dq.size()) qry.pop_front();
}
if (i >= k) dp[i] = dp[i-k] + v[dq.front()];
else dp[i] = v[dq.front()];
if (qry.size()) dp[i] = std::min(dp[i], qry.op());
}
constexpr const i64 M = 1000000007;
i64 x = 0;
for (size_t i = 0; i < n; ++i) x = (x * 23ll % M + dp[i]) % M;
delete []dp;
return int(x);
}
Compilation message
peru.cpp: In function 'int solve(int, int, int*)':
peru.cpp:200:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
200 | for (size_t i = 0; i < n; ++i) x = (x * 23ll % M + dp[i]) % M;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
472 KB |
Output is correct |
15 |
Correct |
20 ms |
8032 KB |
Output is correct |
16 |
Correct |
20 ms |
8028 KB |
Output is correct |
17 |
Correct |
22 ms |
8016 KB |
Output is correct |
18 |
Correct |
20 ms |
8036 KB |
Output is correct |
19 |
Correct |
21 ms |
8020 KB |
Output is correct |
20 |
Correct |
21 ms |
8012 KB |
Output is correct |
21 |
Correct |
20 ms |
8024 KB |
Output is correct |
22 |
Correct |
21 ms |
8020 KB |
Output is correct |
23 |
Correct |
22 ms |
8172 KB |
Output is correct |
24 |
Correct |
24 ms |
8284 KB |
Output is correct |
25 |
Correct |
23 ms |
8016 KB |
Output is correct |
26 |
Correct |
20 ms |
8024 KB |
Output is correct |
27 |
Correct |
19 ms |
8168 KB |
Output is correct |
28 |
Correct |
18 ms |
8028 KB |
Output is correct |
29 |
Correct |
17 ms |
8284 KB |
Output is correct |
30 |
Correct |
21 ms |
8028 KB |
Output is correct |
31 |
Correct |
25 ms |
8204 KB |
Output is correct |
32 |
Correct |
18 ms |
8480 KB |
Output is correct |
33 |
Correct |
23 ms |
8016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
8032 KB |
Output is correct |
2 |
Correct |
20 ms |
8028 KB |
Output is correct |
3 |
Correct |
22 ms |
8016 KB |
Output is correct |
4 |
Correct |
20 ms |
8036 KB |
Output is correct |
5 |
Correct |
21 ms |
8020 KB |
Output is correct |
6 |
Correct |
21 ms |
8012 KB |
Output is correct |
7 |
Correct |
20 ms |
8024 KB |
Output is correct |
8 |
Correct |
21 ms |
8020 KB |
Output is correct |
9 |
Correct |
22 ms |
8172 KB |
Output is correct |
10 |
Correct |
24 ms |
8284 KB |
Output is correct |
11 |
Correct |
23 ms |
8016 KB |
Output is correct |
12 |
Correct |
20 ms |
8024 KB |
Output is correct |
13 |
Correct |
19 ms |
8168 KB |
Output is correct |
14 |
Correct |
18 ms |
8028 KB |
Output is correct |
15 |
Correct |
17 ms |
8284 KB |
Output is correct |
16 |
Correct |
21 ms |
8028 KB |
Output is correct |
17 |
Correct |
25 ms |
8204 KB |
Output is correct |
18 |
Correct |
18 ms |
8480 KB |
Output is correct |
19 |
Correct |
23 ms |
8016 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
360 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
360 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
472 KB |
Output is correct |
34 |
Correct |
125 ms |
41812 KB |
Output is correct |
35 |
Correct |
109 ms |
42068 KB |
Output is correct |
36 |
Correct |
112 ms |
41900 KB |
Output is correct |
37 |
Correct |
122 ms |
41940 KB |
Output is correct |
38 |
Correct |
118 ms |
42068 KB |
Output is correct |
39 |
Correct |
119 ms |
42268 KB |
Output is correct |
40 |
Correct |
130 ms |
41928 KB |
Output is correct |
41 |
Correct |
134 ms |
41812 KB |
Output is correct |
42 |
Correct |
130 ms |
42212 KB |
Output is correct |
43 |
Correct |
44 ms |
17424 KB |
Output is correct |
44 |
Correct |
75 ms |
26448 KB |
Output is correct |
45 |
Correct |
75 ms |
26448 KB |
Output is correct |
46 |
Correct |
77 ms |
26552 KB |
Output is correct |
47 |
Correct |
113 ms |
43092 KB |
Output is correct |
48 |
Correct |
119 ms |
42968 KB |
Output is correct |
49 |
Correct |
116 ms |
43224 KB |
Output is correct |
50 |
Correct |
105 ms |
43860 KB |
Output is correct |
51 |
Correct |
109 ms |
43600 KB |
Output is correct |
52 |
Correct |
105 ms |
44184 KB |
Output is correct |
53 |
Correct |
106 ms |
43864 KB |
Output is correct |
54 |
Correct |
136 ms |
42020 KB |
Output is correct |
55 |
Correct |
107 ms |
41952 KB |
Output is correct |
56 |
Correct |
100 ms |
41868 KB |
Output is correct |
57 |
Correct |
107 ms |
41944 KB |
Output is correct |
58 |
Correct |
110 ms |
41944 KB |
Output is correct |
59 |
Correct |
112 ms |
30188 KB |
Output is correct |
60 |
Correct |
109 ms |
29780 KB |
Output is correct |
61 |
Correct |
140 ms |
29936 KB |
Output is correct |
62 |
Correct |
126 ms |
29972 KB |
Output is correct |
63 |
Correct |
132 ms |
29988 KB |
Output is correct |