제출 #556265

#제출 시각아이디문제언어결과실행 시간메모리
556265cheissmartRail (IOI14_rail)C++14
100 / 100
78 ms772 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);
			int new_pos = pos[right_up] - tt;
			int flip_pos = *up.upper_bound(new_pos);
			if(da[i] == flip_pos - new_pos + flip_pos - pos[a]) {
				pos[i] = new_pos;
				tp[i] = 1; // down
				down.insert(pos[i]);
			} else {
				pos[i] = pos[a] + da[i];
				tp[i] = 2; // up
				up.insert(pos[i]);
				right_up = i;
			}
		} else { // left
			assert(da[i] - (pos[b] - pos[a]) == db[i]);
			int tt = getDistance(left_down, i);
			int new_pos = pos[left_down] + tt;
			int flip_pos = *prev(down.upper_bound(new_pos));
			if(db[i] == new_pos - flip_pos + pos[b] - flip_pos) {
				pos[i] = new_pos;
				tp[i] = 2; // up
				up.insert(pos[i]);
			} else {
				pos[i] = pos[b] - db[i];
				tp[i] = 1; // down
				down.insert(pos[i]);
				left_down = i;
				continue;
			}	
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...