Submission #838056

# Submission time Handle Problem Language Result Execution time Memory
838056 2023-08-26T07:21:32 Z pavement Shortcut (IOI16_shortcut) C++17
0 / 100
1 ms 212 KB
#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define eb emplace_back

using ll = long long;

struct ds {
	set<pair<ll, ll> > S;
	
	void insert(ll x, ll y) {
		vector<set<pair<ll, ll> >::iterator> to_erase;
		// insert (x, y)
		auto it = S.lower_bound(mp(x, 0ll));
		if (it != S.end() && it->second >= y) {
			return;
		}
		if (it == S.end() || it->first != x) {
			if (it == S.begin()) {
				S.emplace(x, y);
				return;
			}
			--it;
		}
		while (1) {
			if (it->second <= y) {
				to_erase.pb(it);
			} else {
				break;
			}
			if (it == S.begin()) {
				break;
			}
			--it;
		}
		for (auto it2 : to_erase) {
			S.erase(it2);
		}
		S.emplace(x, y);
	}
	ll query(ll k) {
		// max y over all x > k
		auto it = S.upper_bound(mp(k, (ll)1e18));
		return it == S.end() ? -(ll)1e18 : it->second;
	}
};

ll find_shortcut(int n, vector<int> l, vector<int> d, int c) {
	vector<ll> x(n, 0);
	for (int i = 0; i + 1 < n; i++) {
		x[i + 1] = x[i] + (ll)l[i];
	}
	
	ds S;
	
	auto good = [&](ll k) {
		ll min_x = -(ll)1e18, max_x = (ll)1e18, min_y = -(ll)1e18, max_y = (ll)1e18;
		vector<tuple<ll, bool, ll, ll*, int> > case_1_events;
		
		for (int i = 0; i < n; i++) {
			case_1_events.eb(d[i] - x[i], 0, x[i] + d[i], nullptr, i);
			case_1_events.eb(k - d[i] - x[i], 1, -k + c + d[i] - x[i], &min_x, i);
			case_1_events.eb(k - d[i] - x[i], 1, -k + c + d[i] - x[i], &min_y, i);
		}
		
		sort(case_1_events.begin(), case_1_events.end(), [](const tuple<ll, bool, ll, ll*, int> &lhs, const tuple<ll, bool, ll, ll*, int> &rhs) {
			auto lp = mp(get<0>(lhs), get<1>(lhs)), rp = mp(get<0>(rhs), get<1>(rhs));
			return lp > rp;
		});
		
		pair<ll, int> case_1_first_best = mp(-(ll)1e18, -1), case_1_second_best = mp(-(ll)1e18, -1);
		
		for (auto [v, type, offset, addr, idx] : case_1_events) {
			if (type == 0) {
				if (case_1_second_best < mp(offset, idx)) {
					case_1_second_best = mp(offset, idx);
				}
				if (case_1_second_best > case_1_first_best) {
					swap(case_1_first_best, case_1_second_best);
				}
			} else {
				*addr = max(*addr, (case_1_first_best.second == idx ? case_1_second_best.first : case_1_first_best.first) + offset);
			}
		}
		
		{
			S.S.clear();
			for (int j = 1; j < n; j++) {
				S.insert(d[j - 1] - x[j - 1], -(x[j - 1] - d[j - 1]));
				max_x = min(max_x, -S.query(k - d[j] - x[j]) + k - c - d[j] - x[j]);
				max_y = min(max_y, -S.query(k - d[j] - x[j]) + k - c - d[j] + x[j]);
			}
		}
		if (min_x <= max_x && min_y <= max_y) {
			set<ll> pos;
			for (int j = 1; j < n; j++) {
				pos.insert(x[j - 1]);
				ll lb = max(min_x + x[j], min_y - x[j]), rb = min(max_x + x[j], max_y - x[j]);
				auto it = pos.lower_bound(lb);
				if (it != pos.end() && *it <= rb) {
					return true;
				}
			}
			return false;
		} else {
			return false;
		}
	};
	
	ll lo = 0, hi = (ll)1e16, ans = -1;
	while (lo <= hi) {
		ll mid = (lo + hi) / 2;
		if (good(mid)) {
			ans = mid;
			hi = mid - 1;
		} else {
			lo = mid + 1;
		}
	}
	
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 1 ms 212 KB n = 2, 3 is a correct answer
7 Incorrect 1 ms 212 KB n = 3, incorrect answer: jury 29 vs contestant 20
8 Halted 0 ms 0 KB -