Submission #109966

#TimeUsernameProblemLanguageResultExecution timeMemory
109966SaboonPalembang Bridges (APIO15_bridge)C++14
31 / 100
65 ms3184 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;
const ll inf = 1e18;

vector<pair<int, int> > eve;

ll solve(){
	int n = eve.size();
	vector<ll> a(2 * n + 1);
	for (int i = 0; i < n; i++){
		a[2 * i + 0 + 1] = eve[i].first;
		a[2 * i + 1 + 1] = eve[i].second;
	}
	sort(a.begin() + 1, a.end());
	n = 2 * n;
	for (int i = 1; i <= n; i++)
		a[i] += a[i - 1];
	ll answer = inf;
	for (int i = 1; i <= n; i++){
		ll T = a[i] - a[i - 1];
		answer = min(answer, T * (i - (n - i)) - a[i] + (a[n] - a[i])); 
	}
	return answer + eve.size();
}

int main(){
	ios_base::sync_with_stdio(false);
	int k, n;
	cin >> k >> n;
	ll answer = 0;
	for (int i = 0; i < n; i++){
		int x, y;
		char a, b;
		cin >> a >> x >> b >> y;
		if (a == b){
			answer += abs(x - y);
			continue;
		}
		if (a == 'B')
			swap(x, y);
		eve.push_back({x, y});
	}
	if (eve.empty())
		return cout << answer << endl, 0;
	if (k == 1){
		cout << answer + solve() << '\n';
		return 0;
	}
	vector<int> places;
	for (int i = 0; i < eve.size(); i++){
		places.push_back(eve[i].first);
		places.push_back(eve[i].second);
	}
	sort(places.begin(), places.end());
	if (n <= 100){
		int m = places.size();
		ll tot = inf;
		for (int i = 0; i < m; i++){
			for (int j = i + 1; j < m; j++){
				ll sum = 0;
				for (auto k : eve){
					ll tmp = min(abs(k.first - places[i]) + abs(k.second - places[i]),
								abs(k.first - places[j]) + abs(k.second - places[j]));
					sum += tmp;
				}
				tot = min(tot, sum + answer + m / 2);
			}
		}
		return cout << tot << endl, 0;
	}
}

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < eve.size(); i++){
                  ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...