답안 #433389

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433389 2021-06-19T16:40:48 Z ngpin04 Circus (Balkan15_CIRCUS) C++14
100 / 100
1343 ms 25140 KB
#include <bits/stdc++.h>
#include "circus.h"
//#include "grader.cpp"
#define fi first
#define se second
#define mp make_pair		
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
const int Nmax = 1e5 + 5; 	
const int oo = 1e9;

vector <int> a;
vector <int> fval, gval;

int mx[Nmax << 2];
int mn[Nmax << 2];

int d[Nmax][2];
int dp[Nmax];
int n,m;

void solve() {
	priority_queue <pair <int, pair <int, int>>> heap;
	for (int i = 0; i <= n; i++)
	for (int j = 0; j < 2; j++)
		d[i][j] = oo;
	d[n][0] = 0;
	heap.push(mp(0, mp(n, 0)));
	while (heap.size()) {
		int cur = -heap.top().fi;
		int i = heap.top().se.fi;
		int dir = heap.top().se.se;
		heap.pop();
		if (d[i][dir] != cur)
			continue;
		int j = i + ((dir == 0) ? -1 : 1);
		if (j >= 0 && j <= n && mini(d[j][dir], cur + abs(a[i] - a[j]))) 
			heap.push(mp(-d[j][dir], mp(j, dir)));

		j = lower_bound(a.begin(), a.end(), a[i] + cur) - a.begin();
		if (j <= n && mini(d[j][1], a[j] - a[i]))
			heap.push(mp(-d[j][1], mp(j, 1)));

		j = upper_bound(a.begin(), a.end(), a[i] - cur) - a.begin() - 1;
		if (j >= 0 && mini(d[j][0], a[i] - a[j]))
			heap.push(mp(-d[j][0], mp(j, 0)));
	}

	for (int i = 0; i <= n; i++) {
		dp[i] = min(d[i][0], d[i][1]);
		// cerr << dp[i] << " \n"[i == n];
	}
}

void update(int id, int l, int r, int pos, pair <int, int> val) {
	if (l > pos || r < pos)
		return;
	if (l == r)
		return (void) (mini(mn[id], val.fi), maxi(mx[id], val.se));
	int mid = (l + r) >> 1;
	update(id << 1, l, mid, pos, val);
	update(id << 1 | 1, mid + 1, r, pos, val);
	mx[id] = max(mx[id << 1], mx[id << 1 | 1]);
	mn[id] = min(mn[id << 1], mn[id << 1 | 1]); 
}

pair <int, int> get(int id, int l, int r, int u, int v) {
	if (l > v || r < u)
		return mp(oo, -oo);
	if (l >= u && r <= v)
		return mp(mn[id], mx[id]);
	int mid = (l + r) >> 1;
	pair <int, int> lnode = get(id << 1, l, mid, u, v);
	pair <int, int> rnode = get(id << 1 | 1, mid + 1, r, u, v);
	return mp(min(lnode.fi, rnode.fi), max(lnode.se, rnode.se));
}

void init(int N, int M, int P[]) {
	n = N;
	m = M;
	for (int i = 0; i < n; i++)
		a.push_back(P[i]);
	a.push_back(M);
	sort(a.begin(), a.end());
	solve();

	for (int i = 0; i <= n; i++) {
		fval.push_back(a[i] - dp[i]);
		gval.push_back(a[i] + dp[i]);
	}
	sort(fval.begin(), fval.end());
	fval.resize(unique(fval.begin(), fval.end()) - fval.begin());
	
	sort(gval.begin(), gval.end());
	gval.resize(unique(gval.begin(), gval.end()) - gval.begin());

	for (int i = 1; i <= ((n + 1) << 2); i++)
		mn[i] = oo, mx[i] = -oo;

	for (int i = 0; i <= n; i++) {
		int pos, val;
		val = a[i] - dp[i];
		pos = lower_bound(fval.begin(), fval.end(), val) - fval.begin();
		update(1, 0, n, pos, mp(a[i], -oo));
		
		val = a[i] + dp[i];
		pos = lower_bound(gval.begin(), gval.end(), val) - gval.begin();
		update(1, 0, n, pos, mp(oo, a[i]));
	}
}

int minLength(int x) {
	int pos;
	int res = oo;
	pos = lower_bound(fval.begin(), fval.end(), x) - fval.begin();
	res = get(1, 0, n, pos, n).fi - x;

	pos = upper_bound(gval.begin(), gval.end(), x) - gval.begin() - 1;
	if (pos >= 0) 
		mini(res, x - get(1, 0, n, 0, pos).se);

	return res;	
}

Compilation message

grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
   14 |  long long max_code;
      |            ^~~~~~~~
grader.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
grader.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
grader.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   scanf("%d", &d);
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 6600 KB Output is correct
2 Correct 187 ms 6576 KB Output is correct
3 Correct 157 ms 6536 KB Output is correct
4 Correct 161 ms 6604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 4 ms 440 KB Output is correct
6 Correct 4 ms 364 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 4 ms 440 KB Output is correct
6 Correct 4 ms 364 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 691 ms 17744 KB Output is correct
10 Correct 698 ms 10724 KB Output is correct
11 Correct 692 ms 10036 KB Output is correct
12 Correct 717 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 6600 KB Output is correct
2 Correct 187 ms 6576 KB Output is correct
3 Correct 157 ms 6536 KB Output is correct
4 Correct 161 ms 6604 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 4 ms 440 KB Output is correct
10 Correct 4 ms 364 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 164 ms 7524 KB Output is correct
14 Correct 157 ms 7576 KB Output is correct
15 Correct 160 ms 7224 KB Output is correct
16 Correct 162 ms 7276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 6600 KB Output is correct
2 Correct 187 ms 6576 KB Output is correct
3 Correct 157 ms 6536 KB Output is correct
4 Correct 161 ms 6604 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 4 ms 440 KB Output is correct
10 Correct 4 ms 364 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 691 ms 17744 KB Output is correct
14 Correct 698 ms 10724 KB Output is correct
15 Correct 692 ms 10036 KB Output is correct
16 Correct 717 ms 18776 KB Output is correct
17 Correct 164 ms 7524 KB Output is correct
18 Correct 157 ms 7576 KB Output is correct
19 Correct 160 ms 7224 KB Output is correct
20 Correct 162 ms 7276 KB Output is correct
21 Correct 1285 ms 19544 KB Output is correct
22 Correct 1320 ms 19616 KB Output is correct
23 Correct 1323 ms 22796 KB Output is correct
24 Correct 1343 ms 25140 KB Output is correct