답안 #278750

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
278750 2020-08-21T18:09:34 Z arnold518 Circus (Balkan15_CIRCUS) C++14
100 / 100
827 ms 22796 KB
#include "circus.h"
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int MAXN = 3e5;
const int INF = 1e9+100;
 
int N, M, P[MAXN+10];
int dist[MAXN+10];
 
struct Queue
{
	int u, w;
	bool operator < (const Queue &p) const { return w>p.w; }
};

struct BIT
{
	int tree[MAXN+10];
	void init() { for(int i=1; i<=N; i++) tree[i]=INF; }
	void update(int i, int k) { for(; i<=N; i+=(i&-i)) tree[i]=min(tree[i], k); }
	int query(int i) { int ret=INF; for(; i>0; i-=(i&-i)) ret=min(ret, tree[i]); return ret; }
};

BIT bit1, bit2;
vector<int> comp1, comp2;

void init(int _N, int _M, int _P[])
{
	N=_N; M=_M;
	for(int i=1; i<=N; i++) P[i]=_P[i-1];
	sort(P+1, P+N+1);
	N=unique(P+1, P+N+1)-P-1;
	P[0]=M;
 
	for(int i=0; i<=3*N; i++) dist[i]=INF;

	priority_queue<Queue> PQ;
	PQ.push({2*N, M-P[N]}); dist[0]=0;
	while(!PQ.empty())
	{
		Queue now=PQ.top(); PQ.pop();
		if(dist[now.u]<=now.w) continue;
		dist[now.u]=now.w;
		if(now.u<=N)
		{
			int t=upper_bound(P+1, P+N+1, P[now.u]-now.w)-P-1;
			if(t!=0) PQ.push({N+t, P[now.u]-P[t]});

			t=lower_bound(P+1, P+N+1, P[now.u]+now.w)-P;
			if(t!=N+1) PQ.push({N*2+t, P[t]-P[now.u]});
		}
		else if(now.u<=2*N)
		{
			PQ.push({now.u-N, now.w});
			if(now.u!=N+1) PQ.push({now.u-1, now.w+P[now.u-N]-P[now.u-N-1]});
		}
		else
		{
			PQ.push({now.u-2*N, now.w});
			if(now.u!=3*N) PQ.push({now.u+1, now.w+P[now.u+1-2*N]-P[now.u-2*N]});
		}
	}
	//for(int i=1; i<=N; i++) printf("%d ", dist[i]); printf("\n");

	bit1.init(); bit2.init();
	for(int i=1; i<=N; i++) comp1.push_back(P[i]+dist[i]);
	sort(comp1.begin(), comp1.end());
	comp1.erase(unique(comp1.begin(), comp1.end()), comp1.end());

	for(int i=1; i<=N; i++) comp2.push_back(P[i]-dist[i]);
	sort(comp2.begin(), comp2.end());
	comp2.erase(unique(comp2.begin(), comp2.end()), comp2.end());	

	for(int i=1; i<=N; i++)
	{
		int p=lower_bound(comp1.begin(), comp1.end(), P[i]+dist[i])-comp1.begin()+1;
		bit1.update(p, -P[i]);
	}
	for(int i=1; i<=N; i++)
	{
		int p=lower_bound(comp2.begin(), comp2.end(), P[i]-dist[i])-comp2.begin()+1;
		bit2.update(N-p+1, P[i]);
	}
}
 
int minLength(int D)
{
	int ans=M-D;

	int p=upper_bound(comp1.begin(), comp1.end(), D)-comp1.begin();
	ans=min(ans, D+bit1.query(p));

	p=lower_bound(comp2.begin(), comp2.end(), D)-comp2.begin()+1;
	ans=min(ans, bit2.query(N-p+1)-D);

	return ans;
}

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 114 ms 4208 KB Output is correct
2 Correct 115 ms 4208 KB Output is correct
3 Correct 119 ms 4188 KB Output is correct
4 Correct 115 ms 4208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 474 ms 8540 KB Output is correct
10 Correct 424 ms 4984 KB Output is correct
11 Correct 426 ms 4552 KB Output is correct
12 Correct 497 ms 9208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 4208 KB Output is correct
2 Correct 115 ms 4208 KB Output is correct
3 Correct 119 ms 4188 KB Output is correct
4 Correct 115 ms 4208 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 117 ms 4208 KB Output is correct
14 Correct 121 ms 4208 KB Output is correct
15 Correct 115 ms 4208 KB Output is correct
16 Correct 118 ms 4336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 4208 KB Output is correct
2 Correct 115 ms 4208 KB Output is correct
3 Correct 119 ms 4188 KB Output is correct
4 Correct 115 ms 4208 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 474 ms 8540 KB Output is correct
14 Correct 424 ms 4984 KB Output is correct
15 Correct 426 ms 4552 KB Output is correct
16 Correct 497 ms 9208 KB Output is correct
17 Correct 117 ms 4208 KB Output is correct
18 Correct 121 ms 4208 KB Output is correct
19 Correct 115 ms 4208 KB Output is correct
20 Correct 118 ms 4336 KB Output is correct
21 Correct 827 ms 15928 KB Output is correct
22 Correct 742 ms 17104 KB Output is correct
23 Correct 769 ms 20352 KB Output is correct
24 Correct 804 ms 22796 KB Output is correct