Submission #585417

# Submission time Handle Problem Language Result Execution time Memory
585417 2022-06-28T20:21:24 Z GusterGoose27 Palembang Bridges (APIO15_bridge) C++11
100 / 100
331 ms 17484 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<ll, ll> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


const ll MAXN = 1e5;
ll k, n, m;
pii intervals[MAXN];
ll ans = 0;
ll best[MAXN];
ll bestrev[MAXN];
ordered_set divs;

struct comp {
	bool operator()(const pii &a, const pii &b) {
		return (a.first+a.second) < (b.first+b.second);
	}
} comp;

void rev() {
	for (ll i = 0; i < n; i++) {
		ll f = intervals[i].first;
		intervals[i].first = 1e9-intervals[i].second;
		intervals[i].second = 1e9-f;
	}
	for (ll i = 0; i < n/2; i++) {
		pii temp = intervals[i];
		intervals[i] = intervals[n-1-i];
		intervals[n-1-i] = temp;
	}
}

void makebests(ll best[]) {
	divs.clear();
	divs.insert(intervals[0].first);
	divs.insert(intervals[0].second);
	ll curbest = intervals[0].second-intervals[0].first;
	best[0] = curbest;
	for (ll i = 1; i < n; i++) {
		//ll prevl = divs.find_by_order(divs.size()/2-1);
		ll prevr = *(divs.find_by_order(divs.size()/2));
		divs.insert(intervals[i].first);
		divs.insert(intervals[i].second);
		assert(divs.size() % 2 == 0);
		// ll curl = divs.find_by_order(divs.size()/2-1);
		// ll curr = divs.find_by_order(divs.size()/2);
		best[i] = best[i-1]+intervals[i].second-intervals[i].first;
		if (intervals[i].first > prevr) best[i] += 2*(intervals[i].first-prevr);
	}
}

ll bash() {
	ll b = -1;
	for (int j = 0; j < 2*n; j++) {
		int pos;
		if (j < n) pos = intervals[j].first;
		else pos = intervals[j-n].second;
		ll cur = 0;
		for (int i = 0; i < n; i++) {
			cur += intervals[i].second-intervals[i].first;
			if (pos > intervals[i].second) cur += 2*(pos-intervals[i].second);
			if (pos < intervals[i].first) cur += 2*(intervals[i].first-pos);
		}
		if (b == -1 || cur < b) b = cur;
	}
	return b;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> k >> m;
	n = 0;
	for (ll i = 0; i < m; i++) {
		char c1, c2; ll l, r;
		cin >> c1 >> l >> c2 >> r;
		if (r < l) {
			ll temp = l;
			l = r;
			r = temp;
		}
		if (c1 == c2) {
			ans += r-l;
		}
		else {
			ans++;
			intervals[n++] = pii(l, r);
		}
	}
	if (n == 0) {
		cout << ans << "\n";
		return 0;
	}
	sort(intervals, intervals+n, comp);
	makebests(best);
	if (k == 1) {
		//ll b = bash();
		//assert(best[n-1] == b);
		cout << (ans+best[n-1]) << "\n";
		return 0;
	}
	rev();
	//sort(intervals, intervals+n, comp);
	makebests(bestrev);
	ll b = best[0]+bestrev[n-2];
	for (ll i = 1; i < n-1; i++) {
		if (best[i]+bestrev[n-2-i] < b) b = best[i]+bestrev[n-2-i];
	}
	cout << (b+ans) << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 135 ms 15064 KB Output is correct
13 Correct 149 ms 17484 KB Output is correct
14 Correct 131 ms 14764 KB Output is correct
15 Correct 91 ms 10444 KB Output is correct
16 Correct 197 ms 16812 KB Output is correct
17 Correct 140 ms 17392 KB Output is correct
18 Correct 82 ms 17132 KB Output is correct
19 Correct 144 ms 17424 KB Output is correct
20 Correct 139 ms 17016 KB Output is correct
21 Correct 149 ms 17352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 207 ms 15944 KB Output is correct
26 Correct 267 ms 15868 KB Output is correct
27 Correct 292 ms 15960 KB Output is correct
28 Correct 290 ms 15948 KB Output is correct
29 Correct 331 ms 16004 KB Output is correct
30 Correct 108 ms 8532 KB Output is correct
31 Correct 217 ms 15956 KB Output is correct
32 Correct 223 ms 15912 KB Output is correct
33 Correct 152 ms 15928 KB Output is correct
34 Correct 217 ms 15948 KB Output is correct
35 Correct 214 ms 15940 KB Output is correct
36 Correct 219 ms 15948 KB Output is correct
37 Correct 228 ms 15912 KB Output is correct