Submission #817538

#TimeUsernameProblemLanguageResultExecution timeMemory
817538hugo_pmComparing Plants (IOI20_plants)C++17
25 / 100
4073 ms10956 KiB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

int N, K;
int add(int x, int y) {
	x += y;
	if (x >= N) x -= N;
	return x;
}

int sub(int x, int y) {
	x -= y;
	if (x < 0) x += N;
	return x;
}
int dist(int x, int y) {
	return min(sub(x,y), sub(y,x));
}

vector<int> relative, height;
const int MAX_GRAPH = 300;
bool graph[MAX_GRAPH][MAX_GRAPH];
void pop(int cur, int &toPut) {
	assert(relative[cur] == 0);
	relative[cur] = -1;
	rep(delta, 1, K) {
		int nxt = sub(cur, delta);
		if (relative[nxt] == 0) pop(nxt, toPut);
	}
	height[cur] = toPut--;
	rep(delta, 1, K) {
		relative[sub(cur, delta)]--;
	}
}
void init(int _k, std::vector<int> _r) {
	K = _k;
	relative = _r;
	N = _r.size();
	//--
	height.assign(N, -1);
	int toPut = N-1;
	while (count(all(height), -1)) {
		for (int i = 0; i < N; ++i) {
			if (relative[i] == 0)
				pop(i, toPut);
		}
	}
	dbg(height);
	if (N <= MAX_GRAPH) {
		rep(i, 0, N) rep(j, 0, N) {
			graph[i][j] = (dist(i,j) < K && height[i] > height[j]);
		}
		rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) {
			graph[i][j] |= (graph[i][k] && graph[k][j]);
		}
	}
}

int compare_plants(int x, int y) {
	int xy = (height[x] > height[y]);
	int yx = (height[y] > height[x]);
	if (N <= MAX_GRAPH) {
		xy = graph[x][y], yx = graph[y][x];
	}
	return xy - yx;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...