답안 #73207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
73207 2018-08-28T04:49:39 Z ainta(#2272) Circus (Balkan15_CIRCUS) C++11
91 / 100
2000 ms 13540 KB
#include "circus.h"
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<set>
#define SZ 131072
#define pii pair<int,int>
#define INF 1000000010
using namespace std;
int DP[110000][2], DD[110000], MM, X[110000];
bool v[110000];
int IT[SZ + SZ + 2][2];
set<pii>Set;

struct point {
	int d, fr;
	pii to;
	point() {}
	point(int d_, int fr_, pii to_) {
		d = d_, fr = fr_, to = to_;
	}
	bool operator<(const point &p)const {
		return d>p.d;
	}
};
priority_queue<point>PQ;

int n;

void Ins(int fr, pii to) {
	int num = to.second, x = to.first;
	if (x < fr) {
		if (DP[num][0] <= fr - x)return;
		DP[num][0] = fr - x;
		PQ.push(point(fr - x, fr, to));
	}
	else {
		if (DP[num][1] <= x - fr)return;
		DP[num][1] = x - fr;
		PQ.push(point(x - fr, fr, to));
	}
}

void Do(int x, int d) {
	pii tp = pii(x - d, n + 1);
	auto it = Set.lower_bound(tp);
	if (it != Set.begin()) {
		it--;
		Ins(x, *it);
	}
	tp = pii(x + d, -1);
	it = Set.lower_bound(tp);
	if (it != Set.end()) {
		Ins(x, *it);
	}
}

void Add(int pv, int a, int b) {
	a += SZ;
	IT[a][pv] = b;
	while (a != 1) {
		a >>= 1;
		IT[a][pv] = min(IT[a * 2][pv], IT[a * 2 + 1][pv]);
	}
}

void init(int N, int M, int P[])
{
	n = N;
	MM = M;
	if (n == 0)return;
	int i;
	for (i = 0; i<N; i++)DP[i][0] = DP[i][1] = INF;
	sort(P, P + N);
	for (i = 0; i<n; i++) {
		Set.insert(pii(P[i], i));
		X[i] = P[i];
	}
	Ins(M, pii(P[n - 1], n - 1));
	point tp;
	while (!PQ.empty()) {
		tp = PQ.top();
		PQ.pop();
		int num = tp.to.second, x = tp.to.first;
		if (x < tp.fr) {
			if (tp.d != DP[num][0])continue;
			if (!v[num]) {
				v[num] = true;
				Do(x, tp.d);
			}
			if (num != 0) {
				Ins(tp.fr, pii(P[num - 1], num - 1));
			}
		}
		else {
			if (tp.d != DP[num][1])continue;
			if (!v[num]) {
				v[num] = true;
				Do(x, tp.d);
			}
			if (num != n - 1) {
				Ins(tp.fr, pii(P[num + 1], num + 1));
			}
		}
	}
	for (i = 0; i<n; i++)DD[i] = min(DP[i][0], DP[i][1]);
	for (i = 0; i<n; i++) {
		Add(0, i, DD[i] + X[i]);
		Add(1, i, DD[i] - X[i]);
	}
}
int Get1(int b, int e, int D) {
	if (b>e)return INF;
	b += SZ, e += SZ;
	while (b <= e) {
		if (IT[e][0] <= D)break;
		b = b >> 1;
		e = (e - 1) >> 1;
	}
	if (b>e)return INF;
	while (e < SZ) {
		e = e * 2;
		if (IT[e + 1][0] <= D)e++;
	}
	return D - X[e - SZ];
}
int Get2(int b, int e, int D) {
	if (b>e)return INF;
	b += SZ, e += SZ;
	while (b <= e) {
		if (IT[b][1] <= D)break;
		b = (b + 1) >> 1;
		e = e >> 1;
	}
	if (b>e)return INF;
	while (b < SZ) {
		b = b * 2;
		if (IT[b][1] > D)b++;
	}
	return X[b - SZ] + D;
}
int minLength(int D)
{
	if (!n)return MM - D;
	auto tp = Set.lower_bound(pii(D, -1));
	int mm, i;
	if (tp == Set.end())mm = n;
	else mm = tp->second;
	int t1 = Get1(0, mm - 1, D), t2 = Get2(mm, n - 1, -D);
	return min(min(t1, t2), MM - D);
}

Compilation message

circus.cpp: In function 'int minLength(int)':
circus.cpp:146:10: warning: unused variable 'i' [-Wunused-variable]
  int mm, i;
          ^
grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
  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]
  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]
   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]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &d);
   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 8696 KB Output is correct
2 Correct 175 ms 8736 KB Output is correct
3 Correct 204 ms 8736 KB Output is correct
4 Correct 167 ms 9016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9016 KB Output is correct
2 Correct 3 ms 9016 KB Output is correct
3 Correct 3 ms 9016 KB Output is correct
4 Correct 3 ms 9016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9016 KB Output is correct
2 Correct 3 ms 9016 KB Output is correct
3 Correct 3 ms 9016 KB Output is correct
4 Correct 3 ms 9016 KB Output is correct
5 Correct 7 ms 9016 KB Output is correct
6 Correct 5 ms 9016 KB Output is correct
7 Correct 7 ms 9016 KB Output is correct
8 Correct 9 ms 9016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9016 KB Output is correct
2 Correct 3 ms 9016 KB Output is correct
3 Correct 3 ms 9016 KB Output is correct
4 Correct 3 ms 9016 KB Output is correct
5 Correct 7 ms 9016 KB Output is correct
6 Correct 5 ms 9016 KB Output is correct
7 Correct 7 ms 9016 KB Output is correct
8 Correct 9 ms 9016 KB Output is correct
9 Correct 808 ms 9016 KB Output is correct
10 Correct 706 ms 9016 KB Output is correct
11 Correct 617 ms 9016 KB Output is correct
12 Correct 749 ms 9316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 8696 KB Output is correct
2 Correct 175 ms 8736 KB Output is correct
3 Correct 204 ms 8736 KB Output is correct
4 Correct 167 ms 9016 KB Output is correct
5 Correct 3 ms 9016 KB Output is correct
6 Correct 3 ms 9016 KB Output is correct
7 Correct 3 ms 9016 KB Output is correct
8 Correct 3 ms 9016 KB Output is correct
9 Correct 7 ms 9016 KB Output is correct
10 Correct 5 ms 9016 KB Output is correct
11 Correct 7 ms 9016 KB Output is correct
12 Correct 9 ms 9016 KB Output is correct
13 Correct 180 ms 9316 KB Output is correct
14 Correct 173 ms 9316 KB Output is correct
15 Correct 175 ms 9316 KB Output is correct
16 Correct 185 ms 9316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 8696 KB Output is correct
2 Correct 175 ms 8736 KB Output is correct
3 Correct 204 ms 8736 KB Output is correct
4 Correct 167 ms 9016 KB Output is correct
5 Correct 3 ms 9016 KB Output is correct
6 Correct 3 ms 9016 KB Output is correct
7 Correct 3 ms 9016 KB Output is correct
8 Correct 3 ms 9016 KB Output is correct
9 Correct 7 ms 9016 KB Output is correct
10 Correct 5 ms 9016 KB Output is correct
11 Correct 7 ms 9016 KB Output is correct
12 Correct 9 ms 9016 KB Output is correct
13 Correct 808 ms 9016 KB Output is correct
14 Correct 706 ms 9016 KB Output is correct
15 Correct 617 ms 9016 KB Output is correct
16 Correct 749 ms 9316 KB Output is correct
17 Correct 180 ms 9316 KB Output is correct
18 Correct 173 ms 9316 KB Output is correct
19 Correct 175 ms 9316 KB Output is correct
20 Correct 185 ms 9316 KB Output is correct
21 Execution timed out 2045 ms 13540 KB Time limit exceeded
22 Halted 0 ms 0 KB -