답안 #1034773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1034773 2024-07-25T17:48:51 Z Tob Palembang Bridges (APIO15_bridge) C++14
22 / 100
85 ms 14008 KB
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const int N = 1e5 + 7;

int n, k;
int l[N], r[N], l_[N], r_[N], ind[N];

int main () {
	FIO;
	cin >> k >> n;
	
	ll res = 0;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		int a, b;
		char c1, c2;
		cin >> c1 >> a >> c2 >> b;
		if (c1 == c2) res += abs(a-b);
		else {
			res++;
			l_[cnt] = min(a, b);
			r_[cnt++] = max(a, b);
		}
	}
	for (int i = 0; i < cnt; i++) ind[i] = i;
	
	sort(ind, ind+cnt, [&](int x, int y) {return l_[x]+r_[x] < l_[y]+r_[y];});
	for (int i = 0; i < cnt; i++) l[i] = l_[ind[i]], r[i] = r_[ind[i]];
	
	if (k == 1) {
		multiset <int> s;
		ll d = -2*cnt, h = 0;
		for (int i = 0; i < cnt; i++) {
			s.insert(l[i]);
			s.insert(r[i]);
			h += l[i]+r[i];
		}
		for (int i = 0; i < cnt; i++) s.erase(--s.end());
		int la = 0;
		for (auto x : s) {
			h += d*(x-la);
			la = x;
			d += 2;
		}
		res += h;
	}
	else {
		set <pii> s;
		map <ll, ll> ma;
		ll y = -1, z = 0, h = 0;
		auto add = [&](ll x) {
			s.insert({x, ma[x]++});
			s.insert({x, ma[x]++});
			if (y == -1) {y = s.begin() -> F; return;}
			h += abs(y-x);
			auto p = s.find({y, z});
			if (x >= y) ++p;
			else --p;
			pii ny = *p;
			h -= abs(y-ny.F);
			y = ny.F;
			z = ny.S;
		};
		vector <ll> pre(cnt+1, 0);
		for (int i = 0; i < cnt; i++) {
			add(l[i]);
			add(r[i]);
			pre[i+1] = h;
		}
		s.clear();
		ma.clear();
		y = -1; z = 0; h = 0;
		ll mn = pre[cnt];
		for (int i = cnt-1; i >= 0; i--) {
			add(l[i]);
			add(r[i]);
			mn = min(mn, pre[i]+h);
		}
		res += mn;
	}
	
	cout << res << "\n";

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 476 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 48 ms 12472 KB Output is correct
13 Correct 85 ms 13904 KB Output is correct
14 Correct 64 ms 11752 KB Output is correct
15 Correct 41 ms 8224 KB Output is correct
16 Correct 47 ms 13392 KB Output is correct
17 Correct 60 ms 14008 KB Output is correct
18 Correct 45 ms 13624 KB Output is correct
19 Correct 62 ms 13904 KB Output is correct
20 Correct 55 ms 13584 KB Output is correct
21 Correct 53 ms 13584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -