Submission #671177

#TimeUsernameProblemLanguageResultExecution timeMemory
671177NothingXDPalembang Bridges (APIO15_bridge)C++17
9 / 100
2081 ms360 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
  struct FastMod {
  ull b, m;
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
  ull q = (ull)((L(m) * a) >> 64);
  ull r = a - q * b; // can be proven that 0 <= r < 2*b
  return r >= b ? r - b : r;
  }
  };
  FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 1e5 + 10;
const ll inf = 1e18;
int n, k, x[maxn], y[maxn];
vector<int> num;
ll res, ans;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> k >> n;

	for (int i = 1; i <= n; i++){
		char s, t; cin >> s >> x[i] >> t >> y[i];
		if (x[i] > y[i]) swap(x[i], y[i]);
		if (s == t){
			res += y[i] - x[i];
			x[i] = y[i] = -1;
			continue;
		}
		num.push_back(x[i]);
		num.push_back(y[i]);
	}

	if (num.empty()) return cout << res << '\n', 0;

	sort(all(num));
	num.resize(distance(num.begin(), unique(all(num))));

	int N = num.size();
	ll ans = inf;

	for (int i = 0; i < N; i++){
		for (int j = i+1; j < N; j++){
			ll sum = 0;
			for (int k = 1; k <= n; k++){
				if (x[k] == -1) continue;
				sum += min(abs(x[k] - num[i]) + abs(y[k] - num[i]), abs(x[k] - num[j]) + abs(y[k] - num[j])) + 1;
			}
			ans = min(ans, sum);
		}
	}

	cout << ans + res << '\n';

	return 0;
}
#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...