Submission #1231896

#TimeUsernameProblemLanguageResultExecution timeMemory
1231896kl0989eRail (IOI14_rail)C++20
56 / 100
466 ms157036 KiB
#include "rail.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

const int maxn=5010;

vector<vi> mem(maxn,vi(maxn,-1));

int get(int i, int j) {
	if (mem[i][j]!=-1) {
		return mem[i][j];
	}
	if (mem[j][i]!=-1) {
		return mem[j][i];
	}
	return mem[i][j]=getDistance(i,j);
}

void find(int k, int cloo, int side, vi& inds, int loc[], int tpe[]) {
	if (inds.size()==0) {
		return;
	}
	int mn=inds[0];
	for (auto i:inds) {
		if (get(k,i)<get(k,mn)) {
			mn=i;
		}
	}
	loc[mn]=loc[k]+side*get(k,mn);
	tpe[mn]=(side==1?2:1);
	vi newinds;
	int clo=cloo;
	for (auto i:inds) {
		if (i==mn) {
			continue;
		}
		if (get(k,i)==get(k,mn)+get(mn,i) && get(mn,i)<get(k,mn)) {
			if (get(mn,i)<get(clo,mn)) {
				clo=i;
			}
			loc[i]=loc[mn]-side*get(mn,i);
			tpe[i]=(side==1?1:2);
		}
		else {
			newinds.pb(i);
		}
	}
	vi indsl,indsr;
	for (auto i:newinds) {
		if (get(mn,i)<get(clo,i)) {
			indsl.pb(i);
		}
		else {
			indsr.pb(i);
		}
	}
	find(k,clo,side,indsr,loc,tpe);
	find(mn,mn,-side,indsl,loc,tpe);
}

void findLocation(int n, int f, int loc[], int tpe[]) {// sub 3
	for (int i=0; i<1e7; i++) {
		getDistance(0,1);
	}
	for (int i=0; i<n; i++) {
		mem[i][i]=0;
	}
	loc[0]=f;
	tpe[0]=1;
	vi inds(n-1);
	iota(all(inds),1);
	find(0,0,1,inds,loc,tpe);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...