Submission #61840

# Submission time Handle Problem Language Result Execution time Memory
61840 2018-07-26T20:35:17 Z kingpig9 Library (JOI18_library) C++11
0 / 100
72 ms 1240 KB
#include <bits/stdc++.h>
#include "library.h"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1010;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

int N;
vector<pii> edges;
int cache[MAXN][MAXN];

vector<int> getvec (int lt, int rt) {
	vector<int> v(N);
	for (int i = lt; i <= rt; i++) {
		v[i] = true;
	}
	return v;
}

int query (int lt, int rt) {
	if (cache[lt][rt]) {
		return cache[lt][rt];
	}

	//how many of them are in the range lt, rt?
	return cache[lt][rt] = (lt == rt ? 1 : Query(getvec(lt, rt)));
}

int pquery[MAXN];

int getnew (int lt, int rt) {
	//what happens when you add rt? how many components does it change by?
	return query(lt, rt) - query(lt, rt - 1);
}

vector<int> adj[MAXN];

void Solve (int nnn) {
	N = nnn;
	if (N == 1) {
		//SPECIAL CASE!!!
		Answer(vector<int> {1});
		return;
	} else if (N == 2) {
		Answer(vector<int> {1, 2});
		return;
	}
	
	for (int rt = 1; rt < N; rt++) {
		//check if 
		int qdiff = getnew(0, rt);
		if (qdiff == +1) {
			//no edges added
		} else if (qdiff == 0) {
			//one edge added -- where is it?
			int lo = 0, hi = rt;	//lo for sure <= other endpt, hi for sure NOT true
			while (hi - lo > 1) {
				int mid = (lo + hi) / 2;
				if (getnew(mid, rt) == 0) {
					lo = mid;
				} else {
					hi = mid;
				}
			}
			edges.push_back({lo, rt});
		} else if (qdiff == -1) {
			//TWO edges added!!!
			int lo = 0, hi = rt;
			while (hi - lo > 1) {
				int mid = (lo + hi) / 2;
				if (getnew(mid, rt) < 1) {
					lo = mid;
				} else {
					hi = mid;
				}
			}
			edges.push_back({lo, rt});

			tie(lo, hi) = pii(0, lo);
			while (hi - lo > 1) {
				int mid = (lo + hi) / 2;
				if (getnew(mid, rt) < 0) {
					lo = mid;
				} else {
					hi = mid;
				}
			}
			edges.push_back({lo, rt});
		} else {
			assert(!"Bad qdiff");
		}
	}

	for (pii e : edges) {
		adj[e.fi].push_back(e.se);
		adj[e.se].push_back(e.fi);
	}

	int leaf = 0;
	while (adj[leaf].size() != 1) {
		leaf++;
	}

	vector<int> ans;
	int cur = leaf, prv = 0;
	while (ans.size() < N) {
		ans.push_back(cur + 1);
		for (int i : adj[cur]) {
			if (i != prv) {
				tie(cur, prv) = pii(i, cur);
				break;
			}
		}
	}
	Answer(ans);
}

Compilation message

library.cpp: In function 'void Solve(int)':
library.cpp:114:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while (ans.size() < N) {
         ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 41 ms 1016 KB Output is correct
2 Correct 46 ms 1076 KB Output is correct
3 Correct 58 ms 1076 KB Output is correct
4 Correct 61 ms 1128 KB Output is correct
5 Correct 63 ms 1184 KB Output is correct
6 Correct 69 ms 1184 KB Output is correct
7 Correct 72 ms 1240 KB Output is correct
8 Correct 45 ms 1240 KB Output is correct
9 Correct 56 ms 1240 KB Output is correct
10 Correct 45 ms 1240 KB Output is correct
11 Correct 3 ms 1240 KB Output is correct
12 Correct 2 ms 1240 KB Output is correct
13 Incorrect 2 ms 1240 KB Wrong Answer [6]
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 1016 KB Output is correct
2 Correct 46 ms 1076 KB Output is correct
3 Correct 58 ms 1076 KB Output is correct
4 Correct 61 ms 1128 KB Output is correct
5 Correct 63 ms 1184 KB Output is correct
6 Correct 69 ms 1184 KB Output is correct
7 Correct 72 ms 1240 KB Output is correct
8 Correct 45 ms 1240 KB Output is correct
9 Correct 56 ms 1240 KB Output is correct
10 Correct 45 ms 1240 KB Output is correct
11 Correct 3 ms 1240 KB Output is correct
12 Correct 2 ms 1240 KB Output is correct
13 Incorrect 2 ms 1240 KB Wrong Answer [6]
14 Halted 0 ms 0 KB -