답안 #430757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
430757 2021-06-17T03:38:09 Z Kevin_Zhang_TW 별자리 3 (JOI20_constellation3) C++17
100 / 100
418 ms 59136 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing

const int MAX_N = 200010, MAX_M = 2005;

int n, h[MAX_N], m;
//           y, c
vector<pair<int,int>> star[MAX_N];

//ll dp[MAX_M][MAX_M];

map<int,ll> dp[MAX_N];

ll tg[MAX_N];

// merge to b
void merge_dp(int a, int b) {
	int H = h[b];

	ll alow = 0, blow = 0;
	while (dp[a].size() && dp[a].begin()->first <= H) {
		chmax(alow, dp[a].begin()->second + tg[a]);
		dp[a].erase(dp[a].begin());
	}
	while (dp[b].size() && dp[b].begin()->first <= H) {
		chmax(blow, dp[b].begin()->second + tg[b]);
		dp[b].erase(dp[b].begin());
	}

	tg[a] += blow, tg[b] += alow;

	if (dp[a].size() > dp[b].size()) {
		swap(dp[a], dp[b]), swap(tg[a], tg[b]);
	}
	for (auto [k, w] : dp[a]) {
		chmax(dp[b][k], w + tg[a] - tg[b]);
	}
	
	dp[b][0] = alow + blow - tg[b];
}

int dfs(int l, int r) {
	if (l > r) return n + 1;
	if (l == r) {
		for (auto [y, c] : star[l])
			dp[l][y] = c;
		return l;
	}
	int id = l;
	for (int i = l;i <= r;++i)
		if (h[i] > h[id]) id = i;

	int ld = dfs(l, id-1), rd = dfs(id+1, r);

	for (auto [y, c] : star[id])
		dp[id][y] = c;

	merge_dp(ld, id);
	merge_dp(rd, id);

	return id;
}


int lb[MAX_N], rb[MAX_N], lc[MAX_N], rc[MAX_N];
void dfs(int x) {
	if (x == 0) return;
	dfs(lc[x]), dfs(rc[x]);
	for (auto [y, c] : star[x])
		dp[x][y] = c;

	merge_dp(lc[x], x);
	merge_dp(rc[x], x);
}

ll solve() {

	{
		vector<int> stk;
		for (int i = 1;i <= n;++i) {
			while (stk.size() && h[ stk.back() ] <= h[i])
				stk.pop_back();
			lb[i] = stk.size() ? stk.back() : 0;
			stk.pb(i);
		}
		stk.clear();
		for (int i = n;i >= 1;--i) {
			while (stk.size() && h[ stk.back() ] < h[i])
				stk.pop_back();
			rb[i] = stk.size() ? stk.back() : 0;
			stk.pb(i);
		}
		stk.clear();
	}
	int rt = -1;
	for (int i = 1;i <= n;++i) {
		if (rb[i] == 0 || (lb[i] != 0 && h[ lb[i] ] <= h[ rb[i] ]))
			rc[ lb[i] ] = i;
		else
			lc[ rb[i] ] = i;
		if (rb[i] == 0 && lb[i] == 0) rt = i ;
	}
	assert(rt != -1);

	dfs(rt);
	//int rt = dfs(1, n);

	ll sum = 0;
	for (int i = 1;i <= n;++i)
		for (auto [y, c] : star[i])
			sum += c;

	ll mxg = 0;
	for (auto [_, w] : dp[rt])
		chmax(mxg, w + tg[rt]);

	return sum - mxg;
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1;i <= n;++i)
		cin >> h[i];
	cin >> m;
	for (int x, y, c, i = 0;i < m;++i) {
		cin >> x >> y >> c;
		star[x].pb(y, c);
	}

	cout << solve() << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 9 ms 14468 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 11 ms 14412 KB Output is correct
15 Correct 9 ms 14404 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 9 ms 14468 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 11 ms 14412 KB Output is correct
15 Correct 9 ms 14404 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14376 KB Output is correct
23 Correct 11 ms 14540 KB Output is correct
24 Correct 11 ms 14580 KB Output is correct
25 Correct 10 ms 14596 KB Output is correct
26 Correct 10 ms 14540 KB Output is correct
27 Correct 11 ms 14640 KB Output is correct
28 Correct 10 ms 14540 KB Output is correct
29 Correct 11 ms 14540 KB Output is correct
30 Correct 11 ms 14620 KB Output is correct
31 Correct 10 ms 14540 KB Output is correct
32 Correct 10 ms 14796 KB Output is correct
33 Correct 11 ms 14756 KB Output is correct
34 Correct 13 ms 14772 KB Output is correct
35 Correct 11 ms 14688 KB Output is correct
36 Correct 10 ms 14784 KB Output is correct
37 Correct 10 ms 14796 KB Output is correct
38 Correct 9 ms 14668 KB Output is correct
39 Correct 10 ms 14668 KB Output is correct
40 Correct 10 ms 14796 KB Output is correct
41 Correct 10 ms 14668 KB Output is correct
42 Correct 10 ms 14628 KB Output is correct
43 Correct 11 ms 14824 KB Output is correct
44 Correct 10 ms 14668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 9 ms 14468 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14416 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 11 ms 14412 KB Output is correct
15 Correct 9 ms 14404 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14376 KB Output is correct
23 Correct 11 ms 14540 KB Output is correct
24 Correct 11 ms 14580 KB Output is correct
25 Correct 10 ms 14596 KB Output is correct
26 Correct 10 ms 14540 KB Output is correct
27 Correct 11 ms 14640 KB Output is correct
28 Correct 10 ms 14540 KB Output is correct
29 Correct 11 ms 14540 KB Output is correct
30 Correct 11 ms 14620 KB Output is correct
31 Correct 10 ms 14540 KB Output is correct
32 Correct 10 ms 14796 KB Output is correct
33 Correct 11 ms 14756 KB Output is correct
34 Correct 13 ms 14772 KB Output is correct
35 Correct 11 ms 14688 KB Output is correct
36 Correct 10 ms 14784 KB Output is correct
37 Correct 10 ms 14796 KB Output is correct
38 Correct 9 ms 14668 KB Output is correct
39 Correct 10 ms 14668 KB Output is correct
40 Correct 10 ms 14796 KB Output is correct
41 Correct 10 ms 14668 KB Output is correct
42 Correct 10 ms 14628 KB Output is correct
43 Correct 11 ms 14824 KB Output is correct
44 Correct 10 ms 14668 KB Output is correct
45 Correct 294 ms 34048 KB Output is correct
46 Correct 228 ms 39136 KB Output is correct
47 Correct 256 ms 39948 KB Output is correct
48 Correct 297 ms 38952 KB Output is correct
49 Correct 236 ms 39272 KB Output is correct
50 Correct 247 ms 38844 KB Output is correct
51 Correct 243 ms 39268 KB Output is correct
52 Correct 241 ms 39824 KB Output is correct
53 Correct 272 ms 39160 KB Output is correct
54 Correct 366 ms 57676 KB Output is correct
55 Correct 364 ms 55012 KB Output is correct
56 Correct 418 ms 53476 KB Output is correct
57 Correct 348 ms 53180 KB Output is correct
58 Correct 353 ms 58120 KB Output is correct
59 Correct 330 ms 58388 KB Output is correct
60 Correct 154 ms 54980 KB Output is correct
61 Correct 252 ms 47360 KB Output is correct
62 Correct 349 ms 59136 KB Output is correct
63 Correct 249 ms 44960 KB Output is correct
64 Correct 250 ms 46720 KB Output is correct
65 Correct 350 ms 58948 KB Output is correct
66 Correct 280 ms 45860 KB Output is correct