Submission #41427

#TimeUsernameProblemLanguageResultExecution timeMemory
41427festPalembang Bridges (APIO15_bridge)C++14
22 / 100
69 ms1764 KiB
// fest
#include <bits/stdc++.h>	

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)

typedef long long ll;
typedef long double ld;    

using namespace std;

void FREOPEN() {
	#ifdef COMP
		freopen(".in", "r", stdin);
		freopen("2.out", "w", stdout);
	#else
		freopen(FILENAME".in", "r", stdin);
		freopen(FILENAME".out", "w", stdout);
	#endif
}                           

inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }             

const int N = 200500, inf = 1e9 + 1, MOD = (int)1e9 + 7;

char CH[N];

const ll INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

vector<pair<int, int> > all;

ll calc(int x, int y) {
	ll ret = 0;
	for (auto i : all) {
		ret += min(abs(i.F - x) + abs(i.S - x), abs(i.F - y) + abs(i.S - y));
	}	
	return ret;
}

ll check(int k, int p) {
	if (k == 1) return calc(p, p);
	int l = 0;
	int r = inf;
	while (r - l > 1) {
		int mid = (l + r) / 2;
		if (calc(p, mid) > calc(p, mid + 1)) l = mid;
		else r = mid;
	}
	return min(calc(p, l), calc(p, r));
}

int main() {
  
	int k, n;
	cin >> k >> n;
	ll un = 0;
	for (int i = 1; i <= n; i++) {
		char t1, t2;
		int p1, p2;
		scanf("\n%c %d %c %d", &t1, &p1, &t2, &p2);
		if (t2 == 'A') swap(t1, t2), swap(p1, p2);
		if (t1 == t2) {
			un += abs(p2 - p1);
			continue;
		}
		all.pb({p1, p2});
	}
	if (k == 1) {
		int l = 0;
		int r = inf;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (check(k, mid) > check(k, mid + 1)) l = mid;
			else r = mid;
		}
		cout << un + SZ(all) + min(check(k, l), check(k, r));
		return 0;
	}
	ll mn = INF;
	for (auto i : all) {
		for (auto j : all) {
			mn = min(mn, calc(i.F, j.F));
			mn = min(mn, calc(i.F, j.S));
			mn = min(mn, calc(i.S, j.F));
			mn = min(mn, calc(i.S, j.S));
		}		
	}
	cout << mn + un + SZ(all);
	return 0;	
}

Compilation message (stderr)

bridge.cpp: In function 'void FREOPEN()':
bridge.cpp:25:37: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(FILENAME".in", "r", stdin);
                                     ^
bridge.cpp:26:39: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(FILENAME".out", "w", stdout);
                                       ^
bridge.cpp: In function 'int main()':
bridge.cpp:71:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("\n%c %d %c %d", &t1, &p1, &t2, &p2);
                                             ^
#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...