Submission #556261

#TimeUsernameProblemLanguageResultExecution timeMemory
556261cheissmartRail (IOI14_rail)C++14
30 / 100
80 ms712 KiB
#include "rail.h"
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

void findLocation(int n, int fst_pos, int pos[], int tp[]) {
	for(int i = 0; i < n; i++) tp[i] = 0;
	set<int> up, down;

	int a = 0;
	pos[a] = fst_pos;
	tp[a] = 1; // down
	down.insert(pos[a]);

	if(n == 1) return;

	vi da(n);
	for(int i = 0; i < n; i++) if(i != a)
		da[i] = getDistance(a, i);

	da[a] = INF;
	int b = int(min_element(ALL(da)) - da.begin());
	da[a] = 0;

	pos[b] = pos[a] + da[b];
	tp[b] = 2; // up
	up.insert(pos[b]);

	vi db(n);
	for(int i = 0; i < n; i++) if(i != b)
		db[i] = getDistance(b, i);

	for(int i = 0; i < n; i++) if(i != b && db[i] < db[a] && da[i] - (pos[b] - pos[a]) == db[i]) {
		pos[i] = pos[b] - db[i];
		tp[i] = 1; // down
		down.insert(pos[i]);
	}

	vi p(n); iota(ALL(p), 0);
	sort(ALL(p), [&] (int x, int y) {
		return da[x] < da[y];
	});
	int right_up = b, left_down = a;
	for(int i:p) if(tp[i] == 0) {
		if(da[i] - (pos[b] - pos[a]) < db[i]) { // right
			int tt = getDistance(right_up, i);
			{
				// new right up?
				int new_pos = pos[a] + da[i];
				int flip_pos = *prev(down.upper_bound(pos[right_up]));
				if(pos[right_up] - flip_pos + new_pos - flip_pos == tt) {
					pos[i] = new_pos;
					tp[i] = 2; // up
					up.insert(pos[i]);
					right_up = i;
					continue;
				}
			}
			pos[i] = pos[right_up] - tt;
			tp[i] = 1; // down
			down.insert(pos[i]);
		} else { // left
			assert(da[i] - (pos[b] - pos[a]) == db[i]);
			int tt = getDistance(left_down, i);
			{
				// new left down?
				int new_pos = pos[b] - db[i];
				int flip_pos = *up.upper_bound(pos[left_down]);
				if(i == 4) {
					cerr <<  flip_pos <<endl;
				}
				if(flip_pos - pos[left_down] + flip_pos - new_pos == tt) {
					pos[i] = new_pos;
					tp[i] = 1; // down
					down.insert(pos[i]);
					left_down = i;
					continue;
				}
			}	
			pos[i] = pos[left_down] + tt;
			tp[i] = 2; // up
			up.insert(pos[i]);
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...