답안 #48068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48068 2018-05-10T02:35:13 Z jacobtpl 도서관 (JOI18_library) C++14
100 / 100
489 ms 716 KB
#include "library.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ii pair<int,int>
#define INF 1000000000
#define INFLL 1000000000000000100ll
#define UQ(x) (x).resize(distance((x).begin(),unique(all((x)))))
#define mid(x,y) (((x)+(y))>>1)
#define M 1000000007ll
int n;
int lquery(vector<int> v) {
	vector<int> m(n,0);
	for (int x:v) {
		m[x]=1;
	}
	return Query(m);
}
vector<vector<int> > v;
int rquery(int i,int j) {
	vector<int> m(n,0);
	for (int k=i;k<=j;k++) {
		for (int x:v[k]) m[x]=1;
	}
	return Query(m);
}
int query2(int a,int b) {
	vector<int> m(n,0);
	m[a]=1;
	m[b]=1;
	return Query(m);
}
vector<int> merge(int a,int b) {
	vector<int> x;
	if (sz(v[a])==1 && sz(v[b])==1) {
		x.pb(v[a][0]);
		x.pb(v[b][0]);
		return x;
	}
	if (sz(v[a])>sz(v[b])) swap(a,b);
	if (sz(v[a])==1) {
		if (query2(v[a][0],v[b][0])==1) {
			x.pb(v[a][0]);
			for (int y:v[b]) x.pb(y);
		} else {
			//*******
			//assert(query2(v[a][0],v[b].back())==1);
			//*******
			for (int y:v[b]) x.pb(y);
			x.pb(v[a][0]);
		}
		return x;
	}
	if (query2(v[a][0],v[b][0])==1) {
		for (int y:v[a]) x.pb(y);
		reverse(all(x));
		for (int y:v[b]) x.pb(y);
	} else if (query2(v[a].back(),v[b].back())==1) {
		for (int y:v[a]) x.pb(y);
		for (int i=sz(v[b])-1;i>=0;i--) x.pb(v[b][i]);
	} else if (query2(v[a][0],v[b].back())==1) {
		for (int y:v[a]) x.pb(y);
		reverse(all(x));
		for (int i=sz(v[b])-1;i>=0;i--) x.pb(v[b][i]);
	} else {
		//*******
		//assert(query2(v[a].back(),v[b][0])==1);
		//*******
		for (int y:v[a]) x.pb(y);
		for (int y:v[b]) x.pb(y);
	}
	return x;
}
void Solve(int N) {
	n=N;
	for (int i=0;i<n;i++) {
		vector<int> x;
		x.pb(i);
		v.pb(x);
	}
	#ifdef DEBUG
	for (vector<int> x:v) {
		printf("{ ");
		for (int y:x) printf("%d ", y+1);
		printf("} ");
	}
	printf("\n");
	#endif
	while(sz(v)>1) {
		int b=0,e=sz(v)-1;
		int idx=e;
		while(b<e+1) {
			int m=(b+e)/2;
			int r=rquery(0,m);
			if (r<(m+1)) {
				idx=min(idx,m);
				e=m-1;
			} else b=m+1;
		}
		b=0,e=idx;
		int idx2=0;
		while(b<e+1) {
			int m=(b+e)/2;
			int r=rquery(m,idx);
			if (r<(idx-m+1)) {
				idx2=max(idx2,m);
				b=m+1;
			} else e=m-1;
		}
		assert(idx2<idx);
		vector<int> x=merge(idx2,idx);
		v.erase(v.begin()+idx);
		v.erase(v.begin()+idx2);
		v.pb(x);
		#ifdef DEBUG
		for (vector<int> x:v) {
			printf("{ ");
			for (int y:x) printf("%d ", y+1);
			printf("} ");
		}
		printf("\n");
		#endif
	}
	assert(sz(v)==1);
	assert(sz(v[0])==n);
	
	vector<int> res(n);
	for(int i = 0; i < n; i++) {
		res[i] = v[0][i]+1;
	}
	Answer(res);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 400 KB Output is correct
2 Correct 37 ms 440 KB Output is correct
3 Correct 27 ms 440 KB Output is correct
4 Correct 27 ms 472 KB Output is correct
5 Correct 40 ms 476 KB Output is correct
6 Correct 44 ms 476 KB Output is correct
7 Correct 41 ms 476 KB Output is correct
8 Correct 30 ms 572 KB Output is correct
9 Correct 41 ms 572 KB Output is correct
10 Correct 15 ms 572 KB Output is correct
11 Correct 2 ms 580 KB Output is correct
12 Correct 2 ms 580 KB Output is correct
13 Correct 2 ms 580 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 3 ms 580 KB Output is correct
16 Correct 4 ms 580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 400 KB Output is correct
2 Correct 37 ms 440 KB Output is correct
3 Correct 27 ms 440 KB Output is correct
4 Correct 27 ms 472 KB Output is correct
5 Correct 40 ms 476 KB Output is correct
6 Correct 44 ms 476 KB Output is correct
7 Correct 41 ms 476 KB Output is correct
8 Correct 30 ms 572 KB Output is correct
9 Correct 41 ms 572 KB Output is correct
10 Correct 15 ms 572 KB Output is correct
11 Correct 2 ms 580 KB Output is correct
12 Correct 2 ms 580 KB Output is correct
13 Correct 2 ms 580 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 3 ms 580 KB Output is correct
16 Correct 4 ms 580 KB Output is correct
17 Correct 459 ms 588 KB Output is correct
18 Correct 452 ms 588 KB Output is correct
19 Correct 489 ms 588 KB Output is correct
20 Correct 412 ms 588 KB Output is correct
21 Correct 395 ms 588 KB Output is correct
22 Correct 478 ms 588 KB Output is correct
23 Correct 444 ms 588 KB Output is correct
24 Correct 152 ms 588 KB Output is correct
25 Correct 465 ms 588 KB Output is correct
26 Correct 435 ms 588 KB Output is correct
27 Correct 157 ms 588 KB Output is correct
28 Correct 357 ms 644 KB Output is correct
29 Correct 311 ms 716 KB Output is correct
30 Correct 310 ms 716 KB Output is correct