답안 #968265

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968265 2024-04-23T09:08:20 Z Gromp15 Real Mountains (CCO23_day1problem2) C++17
0 / 25
1 ms 348 KB
#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int mod = 1000003;
void test_case() {
	int n; cin >> n;
	vector<int> h(n);
	int mx = 0;
	for (int &x : h) cin >> x, ckmax(mx, x);
	vector<int> f(h);
	{
		int l = -1, r = -1;
		for (int i = 0; i < n; i++) if (h[i] == mx) {
			if (!~l) l = i;
			r = i;
		}
		for (int i = l; i <= r; i++) f[i] = mx;
		for (int i = 1; i < l; i++) ckmax(f[i], f[i-1]);
		for (int i = n-2; i > r; i--) ckmax(f[i], f[i+1]);
	}
	int ans = 0;
	const int INF = 1e9;
	for (int t = 1; t < mx; t++) {
		vector<int> pref(n), suff(n);
		for (int i = 0; i < n; i++) {
			pref[i] = min(h[i] > t ? h[i] : INF, i ? pref[i-1] : INF);
		}
		for (int i = n-1; i >= 0; i--) {
			suff[i] = min(h[i] > t ? h[i] : INF, i + 1 < n ? suff[i+1] : INF);
		}
		int L = -1, R = -1, bet = 0;
		for (int i = 0; i < n; i++) if (h[i] == t && h[i] < f[i]) {
			if (!~L) L = i;
			R = i;
			bet++;
		}
		if (!~L) continue;
		if (L == R) {
			ans += (pref[L] + suff[L] + h[L]) % mod;
			if (ans >= mod) ans -= mod;
		}
		else {
			if (pref[L] + suff[L] + t + suff[R] + t + 1 + t < pref[R] + suff[R] + t + pref[L] + t + 1 + t) {
				// choose left first
				ans += (pref[L] + suff[L] + h[L]) % mod;
				if (ans >= mod) ans -= mod;
				h[L]++;
				ans += (suff[R] + 2 * t + 1) % mod;
				if (ans >= mod) ans -= mod;
				h[R]++;
			}
			else {
				ans += (pref[R] + suff[R] + h[R]) % mod;
				if (ans >= mod) ans -= mod;
				h[R]++;
				ans += (pref[L] + t + 1 + t) % mod;
				if (ans >= mod) ans -= mod;
				h[L]++;
			}
			ans += (ll)max(bet - 2, 0) * (t + 2 * t + 1) % mod;
			if (ans >= mod) ans -= mod;
			for (int i = 0; i < n; i++) if (h[i] == t && h[i] < f[i]) h[i]++;
		}
	}
	cout << ans << '\n';



}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
//    cin >> t;
    while (t--) test_case();
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -