답안 #671625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
671625 2022-12-13T11:40:50 Z Radin_Zahedi2 Palembang Bridges (APIO15_bridge) C++17
22 / 100
54 ms 3412 KB
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;


int n, k;
const int N = 1e5 + 5;
ll arr[N][2];
pair<int, int> have[2 * N];

ll base = 0;

bool cmp(pair<int, int> &a, pair<int, int> &b) {
	return mp(arr[a.fi][a.se], a.se) < mp(arr[b.fi][b.se], b.se);
}

void init() {
}

void input() {
	cin >> k >> n;

	for (int i = 1; i <= n; i++) {
		char a, b;
		cin >> a >> arr[i][0] >> b >> arr[i][1];

		base += abs(arr[i][0] - arr[i][1]);

		if (a == b) {
			i--;
			n--;
		}
		else {
			if (arr[i][0] > arr[i][1]) {
				swap(arr[i][0], arr[i][1]);
			}
			base++;
		}
	}
}

void solve1() {
	if (k != 1) {
		return;
	}

	if (!n) {
		cout << base;
		return;
	}


	for (int i = 1; i <= n; i++) {
		have[2 * i - 1] = mp(i, 0);
		have[2 * i] = mp(i, 1);
	}
	sort(have + 1, have + 2 * n + 1, cmp);


	ll sum = 0;
	int cnt = -n + 1;
	for (int i = 2; i <= 2 * n; i++) {
		if (have[i].se == 0) {
			sum += arr[have[i].fi][have[i].se];
		}
	}

	ll ans = linf;
	for (int i = 1; i <= 2 * n; i++) {
		int x = arr[have[i].fi][have[i].se];

		ll now = sum + 1ll * cnt * x;
		ans = min(ans, now);

		if (have[i].se == 1) {
			cnt++;
			sum -= arr[have[i].fi][have[i].se];
		}

		if (i < 2 * n && have[i + 1].se == 0) {
			cnt++;
			sum -= arr[have[i + 1].fi][have[i + 1].se];
		}
	}

	cout << base + 2 * ans;
}

int x(int i) {
	return arr[have[i].fi][have[i].se];
}

void solve2() {
	if (k != 2) {
		return;
	}


	for (int i = 1; i <= n; i++) {
		have[2 * i - 1] = mp(i, 0);
		have[2 * i] = mp(i, 1);
	}
	sort(have + 1, have + 2 * n + 1, cmp);


	ll ans = 0;

	int cnt1 = 0;
	int cnt2 = 0;
	ll sum = 0;
	for (int i = 1; i <= 2 * n; i++) {
		if (x(i) > x(1) && have[i].se == 0) {
			sum += x(i);
			cnt2--;
		}
	}

	set<pair<int, int>> betw;
	int i = 1;
	int j = 1;
	while (i < 2 * n) {
		ll now = sum + 1ll * cnt1 * x(i) + 1ll * cnt2 * x(j);
		ans = min(ans, now);
//		cout << x(i) << ' ' << have[i].fi << ' ' << have[i].se << ' ' << i << ' ' << j << ' ' << now << endl;
		while (j < 2 * n && cnt2 <= 0) {
			if (have[j].se == 1) {
				int ind = have[i].fi;
				if (x(i) < arr[ind][0]) {
					betw.insert(mp(arr[ind][0] + arr[ind][1], ind));

					sum -= arr[ind][1];
					cnt2++;
				}
			}

			j++;

			if (have[j].se == 0) {
				sum -= x(j);
				cnt2++;
			}

			while (!betw.empty()) {
				if (betw.begin()->fi > x(i) + x(j)) {
					break;
				}
				
				sum += arr[betw.begin()->se][1];
				cnt2--;

				sum += arr[betw.begin()->se][0];
				cnt1--;
			}
		}


		if (have[i].se == 1) {
			sum -= x(i);
			cnt1++;
		}

		i++;

		if (have[i].se == 0) {
			sum -= x(i);
			cnt1++;
		}


		while (!betw.empty()) {
			if (betw.begin()->fi > x(i) + x(j)) {
				break;
			}
				
			sum += arr[betw.begin()->se][1];
			cnt2--;

			sum += arr[betw.begin()->se][0];
			cnt1--;
		}
	}


	cout << base + 2 * ans;
}

void output() {
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int number_of_testcases = 1;
	//cin >> number_of_testcases;
	while (number_of_testcases--) {
		init();

		input();

		solve1();
		solve2();

		output();
	}

	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 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
# 결과 실행 시간 메모리 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 1 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 26 ms 3348 KB Output is correct
13 Correct 54 ms 3404 KB Output is correct
14 Correct 41 ms 3108 KB Output is correct
15 Correct 31 ms 2132 KB Output is correct
16 Correct 31 ms 3328 KB Output is correct
17 Correct 34 ms 3380 KB Output is correct
18 Correct 41 ms 3404 KB Output is correct
19 Correct 52 ms 3412 KB Output is correct
20 Correct 33 ms 3356 KB Output is correct
21 Correct 44 ms 3356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 0 ms 332 KB Output isn't correct
5 Halted 0 ms 0 KB -