Submission #954232

# Submission time Handle Problem Language Result Execution time Memory
954232 2024-03-27T13:14:33 Z TAhmed33 Peru (RMI20_peru) C++17
100 / 100
172 ms 70292 KB
#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()) mn = min(mn, 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 {
	 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 () {
	 	if (left.empty()) return right.get();
	 	if (right.empty()) return left.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 <= n; 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]);
		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();
	}
	int ret = 0;
	for (int i = 1; i <= n; i++) ret = add(ret, mul(dp[i] % MOD, pw[n - i]));
	return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 23 ms 15132 KB Output is correct
16 Correct 23 ms 14940 KB Output is correct
17 Correct 24 ms 14940 KB Output is correct
18 Correct 26 ms 14928 KB Output is correct
19 Correct 25 ms 14832 KB Output is correct
20 Correct 24 ms 14940 KB Output is correct
21 Correct 23 ms 14940 KB Output is correct
22 Correct 24 ms 14940 KB Output is correct
23 Correct 22 ms 15180 KB Output is correct
24 Correct 23 ms 14940 KB Output is correct
25 Correct 24 ms 14932 KB Output is correct
26 Correct 22 ms 14940 KB Output is correct
27 Correct 21 ms 14916 KB Output is correct
28 Correct 25 ms 15412 KB Output is correct
29 Correct 24 ms 15172 KB Output is correct
30 Correct 22 ms 15140 KB Output is correct
31 Correct 23 ms 14940 KB Output is correct
32 Correct 21 ms 15192 KB Output is correct
33 Correct 23 ms 14840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15132 KB Output is correct
2 Correct 23 ms 14940 KB Output is correct
3 Correct 24 ms 14940 KB Output is correct
4 Correct 26 ms 14928 KB Output is correct
5 Correct 25 ms 14832 KB Output is correct
6 Correct 24 ms 14940 KB Output is correct
7 Correct 23 ms 14940 KB Output is correct
8 Correct 24 ms 14940 KB Output is correct
9 Correct 22 ms 15180 KB Output is correct
10 Correct 23 ms 14940 KB Output is correct
11 Correct 24 ms 14932 KB Output is correct
12 Correct 22 ms 14940 KB Output is correct
13 Correct 21 ms 14916 KB Output is correct
14 Correct 25 ms 15412 KB Output is correct
15 Correct 24 ms 15172 KB Output is correct
16 Correct 22 ms 15140 KB Output is correct
17 Correct 23 ms 14940 KB Output is correct
18 Correct 21 ms 15192 KB Output is correct
19 Correct 23 ms 14840 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 2 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 139 ms 42824 KB Output is correct
35 Correct 135 ms 66756 KB Output is correct
36 Correct 123 ms 66388 KB Output is correct
37 Correct 130 ms 66356 KB Output is correct
38 Correct 126 ms 66620 KB Output is correct
39 Correct 124 ms 66640 KB Output is correct
40 Correct 150 ms 66596 KB Output is correct
41 Correct 138 ms 66372 KB Output is correct
42 Correct 130 ms 66624 KB Output is correct
43 Correct 49 ms 32828 KB Output is correct
44 Correct 91 ms 46664 KB Output is correct
45 Correct 100 ms 46652 KB Output is correct
46 Correct 91 ms 46672 KB Output is correct
47 Correct 128 ms 68708 KB Output is correct
48 Correct 133 ms 68688 KB Output is correct
49 Correct 127 ms 68952 KB Output is correct
50 Correct 127 ms 69716 KB Output is correct
51 Correct 132 ms 69208 KB Output is correct
52 Correct 136 ms 70292 KB Output is correct
53 Correct 130 ms 69720 KB Output is correct
54 Correct 155 ms 66632 KB Output is correct
55 Correct 127 ms 66496 KB Output is correct
56 Correct 132 ms 66496 KB Output is correct
57 Correct 145 ms 66820 KB Output is correct
58 Correct 136 ms 66492 KB Output is correct
59 Correct 128 ms 66540 KB Output is correct
60 Correct 136 ms 66376 KB Output is correct
61 Correct 172 ms 68692 KB Output is correct
62 Correct 141 ms 68764 KB Output is correct
63 Correct 144 ms 68636 KB Output is correct