답안 #876546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
876546 2023-11-21T23:12:22 Z MinaRagy06 Peru (RMI20_peru) C++17
100 / 100
374 ms 77264 KB
#include <bits/stdc++.h>
#include "peru.h"
#ifdef MINA
#include "grader.cpp"
#endif
using namespace std;
#define ll long long

const int N = 2'500'005, mod = 1e9 + 7;
const ll inf = 1e18;
struct Stack {
	stack<array<ll, 2>> s;
	//{mn, actual}
	Stack() {
		s.push({inf, inf});
	};
	void push(ll x) {
		s.push((array<ll, 2>) {min(s.top()[0], x), x});
	}
	ll pop() {
		ll x = s.top()[1];
		s.pop();
		return x;
	}
	bool empty() {
		return s.size() == 1;
	}
	int size() {
		return (int)s.size() - 1;
	}
	ll getmn() {
		return s.top()[0];
	}
	ll top() {
		return s.top()[1];
	}
};
struct Deque {
	Stack l, r;
	void process() {
		int doswap = 0;
		if (r.empty()) {
			doswap = 1;
			swap(l, r);
		}
		int sz = r.size() / 2;
		Stack temp;
		while (sz--) {
			temp.push(r.pop());
		}
		while (!r.empty()) {
			l.push(r.pop());
		}
		while (!temp.empty()) {
			r.push(temp.pop());
		}
		if (doswap) {
			swap(l, r);
		}
	}
	void push_back(ll x) {
		r.push(x);
	}
	void push_front(ll x) {
		l.push(x);
	}
	void pop_back() {
		if (r.empty()) {
			process();
		}
		r.pop();
	}
	void pop_front() {
		if (l.empty()) {
			process();
		}
		l.pop();
	}
	ll back() {
		if (r.empty()) {
			process();
		}
		return r.top();
	}
	ll front() {
		if (l.empty()) {
			process();
		}
		return l.top();
	}
	ll getmn() {
		return min(l.getmn(), r.getmn());
	}
	int size() {
		return l.size() + r.size();
	}
};

ll dp[N], a[N];
Deque mndp, ans, active;
int solve(int n, int k, int *v) {
	for (int i = 0; i < n; i++) {
		a[i + 1] = v[i];
	}
	deque<int> s;
	bool bad = 0;
	auto addbad = [&] (int l) {
		int r = l - 1;
		while (r < s[0]) {
			r++;
			active.push_back(dp[r - 1]);
		}
		ans.pop_front();
		mndp.pop_front();
	};
	for (int i = 1; i <= n; i++) {
		dp[i] = inf;
		ll mn = dp[i - 1];
		while (s.size() && a[i] >= a[s.back()]) {
			mn = min(mn, mndp.back());
			if (s.size() > 1 || !bad) {
				mndp.pop_back();
				ans.pop_back();
			} else if (s.size() == 1 && bad) {
				int r = s.back();
				while (r < i) {
					r++;
					active.push_back(dp[r - 1]);
				}
			}
			s.pop_back();
		}
		s.push_back(i);
		mndp.push_back(mn);
		if (s.size() > 1 || i - k < 1) {
			ans.push_back(a[i] + mn);
		}
		if (i - k >= 0) {
			if (i - k != 0) {
				active.pop_front();
			}
			if (i - k == s.front() || i - k == 0) {
				if (i - k != 0) {
					s.pop_front();
				}
				bad = 1;
				addbad(i - k + 1);
			}
		}
		dp[i] = ans.getmn();
		dp[i] = min(dp[i], active.getmn() + a[s[0]]);
// 		ll c = 0;
// 		for (int j = i; j > max(0, i - k); j--) {
// 			c = max(c, a[j]);
// 			dp[i] = min(dp[i], dp[j - 1] + c);
// 		}
	}
	int ret = 0;
	ll pw = 1;
	for (int i = n; i >= 1; i--) {
		ret += pw * (dp[i] % mod) % mod;
		ret %= mod;
		pw = pw * 23 % mod;
	}
	return ret;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 46 ms 15420 KB Output is correct
16 Correct 44 ms 15184 KB Output is correct
17 Correct 51 ms 15292 KB Output is correct
18 Correct 41 ms 15364 KB Output is correct
19 Correct 41 ms 15352 KB Output is correct
20 Correct 43 ms 15444 KB Output is correct
21 Correct 50 ms 17408 KB Output is correct
22 Correct 51 ms 17100 KB Output is correct
23 Correct 48 ms 17092 KB Output is correct
24 Correct 55 ms 16948 KB Output is correct
25 Correct 63 ms 17152 KB Output is correct
26 Correct 46 ms 17140 KB Output is correct
27 Correct 41 ms 16976 KB Output is correct
28 Correct 41 ms 16984 KB Output is correct
29 Correct 40 ms 17332 KB Output is correct
30 Correct 48 ms 17156 KB Output is correct
31 Correct 52 ms 16980 KB Output is correct
32 Correct 41 ms 17232 KB Output is correct
33 Correct 53 ms 16964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 15420 KB Output is correct
2 Correct 44 ms 15184 KB Output is correct
3 Correct 51 ms 15292 KB Output is correct
4 Correct 41 ms 15364 KB Output is correct
5 Correct 41 ms 15352 KB Output is correct
6 Correct 43 ms 15444 KB Output is correct
7 Correct 50 ms 17408 KB Output is correct
8 Correct 51 ms 17100 KB Output is correct
9 Correct 48 ms 17092 KB Output is correct
10 Correct 55 ms 16948 KB Output is correct
11 Correct 63 ms 17152 KB Output is correct
12 Correct 46 ms 17140 KB Output is correct
13 Correct 41 ms 16976 KB Output is correct
14 Correct 41 ms 16984 KB Output is correct
15 Correct 40 ms 17332 KB Output is correct
16 Correct 48 ms 17156 KB Output is correct
17 Correct 52 ms 16980 KB Output is correct
18 Correct 41 ms 17232 KB Output is correct
19 Correct 53 ms 16964 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4440 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 289 ms 73648 KB Output is correct
35 Correct 252 ms 73288 KB Output is correct
36 Correct 261 ms 73252 KB Output is correct
37 Correct 290 ms 73296 KB Output is correct
38 Correct 285 ms 73280 KB Output is correct
39 Correct 273 ms 73312 KB Output is correct
40 Correct 316 ms 73148 KB Output is correct
41 Correct 328 ms 73300 KB Output is correct
42 Correct 308 ms 73384 KB Output is correct
43 Correct 101 ms 34764 KB Output is correct
44 Correct 156 ms 50984 KB Output is correct
45 Correct 164 ms 50964 KB Output is correct
46 Correct 154 ms 50976 KB Output is correct
47 Correct 257 ms 75396 KB Output is correct
48 Correct 302 ms 75684 KB Output is correct
49 Correct 252 ms 75604 KB Output is correct
50 Correct 242 ms 76740 KB Output is correct
51 Correct 255 ms 76100 KB Output is correct
52 Correct 250 ms 77264 KB Output is correct
53 Correct 241 ms 76624 KB Output is correct
54 Correct 329 ms 73184 KB Output is correct
55 Correct 241 ms 73036 KB Output is correct
56 Correct 243 ms 73468 KB Output is correct
57 Correct 248 ms 73220 KB Output is correct
58 Correct 262 ms 73128 KB Output is correct
59 Correct 262 ms 73176 KB Output is correct
60 Correct 260 ms 73216 KB Output is correct
61 Correct 374 ms 75492 KB Output is correct
62 Correct 314 ms 75460 KB Output is correct
63 Correct 325 ms 75448 KB Output is correct