Submission #168862

# Submission time Handle Problem Language Result Execution time Memory
168862 2019-12-16T17:31:44 Z Mamnoon_Siam Cipele (COCI18_cipele) C++17
90 / 90
629 ms 5000 KB
#include  <bits/stdc++.h>
using namespace std;

using ll = long long;
using ii = pair<int, int>;

const int maxn = 1e5 + 5;

int n, m;
int a[maxn], b[maxn];
int par[maxn];
int find(int u) {
	return par[u] == u ? u : par[u] = find(par[u]);
}

bool can(int del) {
	vector<ii> v;
	for(int i = 1; i <= n; i++) {
		int l = lower_bound(b + 1, b + 1 + m, a[i] - del) - b;
		int r = upper_bound(b + 1, b + 1 + m, a[i] + del) - b - 1;
		if(l > r) return 0;
		v.emplace_back(l, r);
	}
	sort(v.begin(), v.end());
	iota(par, par + 1 + m + 1, 0);
	for(ii el : v) {
		int l = el.first, r = el.second;
		int mn = find(l);
		if(mn > r) {
			return 0;
		} else {
			par[mn] = mn + 1;
		}
	}
	return 1;
}

int main(int argc, char const *argv[])
{
#ifdef LOCAL
	freopen("in", "r", stdin);
#endif
	cin >> n >> m;
	if(n <= m) {
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		for(int i = 1; i <= m; i++) {
			cin >> b[i];
		}
	} else {
		for(int i = 1; i <= n; i++) {
			cin >> b[i];
		}
		for(int i = 1; i <= m; i++) {
			cin >> a[i];
		}
		swap(n, m);
	}
	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + m);
	int lo = 0, hi = 1e9, opt = -1, mid;
	while(lo <= hi) {
		mid = lo + hi >> 1;
		if(can(mid)) {
			opt = mid;
			hi = mid - 1;
		} else {
			lo = mid + 1;
		}
	}
	cout << opt << endl;
	return 0;
}

Compilation message

cipele.cpp: In function 'int main(int, const char**)':
cipele.cpp:64:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   mid = lo + hi >> 1;
         ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 519 ms 4680 KB Output is correct
2 Correct 619 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 629 ms 5000 KB Output is correct
2 Correct 619 ms 4900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 376 KB Output is correct
2 Correct 19 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 20 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 504 KB Output is correct
2 Correct 20 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 548 KB Output is correct
2 Correct 20 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 504 KB Output is correct
2 Correct 20 ms 476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 438 ms 3924 KB Output is correct
2 Correct 341 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 547 ms 4676 KB Output is correct
2 Correct 329 ms 2856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 442 ms 3968 KB Output is correct
2 Correct 534 ms 4388 KB Output is correct