Submission #108287

#TimeUsernameProblemLanguageResultExecution timeMemory
108287maksim_gaponovPalembang Bridges (APIO15_bridge)C++14
8 / 100
2041 ms4444 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define pb push_back
typedef pair<int, int> pii;
#define F first
#define S second
#define len(x) (int)(x).size()
const int INF = 1e18;

bool cmin(int &a, const int &b) {
	if (a > b) {
		a = b;
		return 1;
	}
	return 0;
}

void run() {
	int k, n;
	cin >> k >> n;
	int ans = 0;
	vector<pii> v;
	vector<int> vals;
	for (int i = 0; i < n; ++i) {
		char a, c;
		int b, d;
		cin >> a >> b >> c >> d;
		a -= 'A';
		c -= 'A';
		if (a == c) {
			ans += abs(b - d);
		} else {
			if (a)
				swap(b, d);
			v.pb({b, d});
			vals.pb(b);
			vals.pb(d);
		}
	}
	int ans2 = INF;
	if (len(vals) == 0)
		ans2 = 0;
	for (auto x : vals) {
		int cur_ans2 = 0;
		for (auto f : v) {
			cur_ans2 += abs(f.F - x) + abs(f.S - x) + 1;
		}
		cmin(ans2, cur_ans2);
	}
	// cout << ans << ' ' << ans2 << '\n';
	cout << ans + ans2 << '\n';
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}
#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...