Submission #2092619


Source Code Expand

#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#define inf 1000000000

using namespace std;
typedef pair<int, int> P;

struct SegTree{
	int size;
	vector<P> seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init(){
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = make_pair(inf, inf);
	}
	
	void update(int i, P val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = min(seg[i*2], seg[i*2+1]);
		}
	}

	P query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return make_pair(inf, inf);
		if(a <= l && r <= b) return seg[k];
		P lval = query(a, b, k*2, l, (l+r)/2);
		P rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return min(lval, rval);
	}
	P query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

SegTree odd(18), even(18);

struct Range{
	int l, r;
	P p1, p2;
	
	Range(){}
	Range(int a, int b){
		l = a, r = b;
		calc();
	}
	void calc(){
		if(l%2){
			p1 = odd.query(l, r);
			p2 = even.query(p1.second+1, r);
		}
		else{
			p1 = even.query(l, r);
			p2 = odd.query(p1.second+1, r);
		}
	}
	bool operator>(const Range& obj)const{
		return p1 > obj.p1;
	}
};

int N;
priority_queue< Range, vector<Range>, greater<Range> > Q;
vector<int> ans;

int main(void)
{
	odd.init(), even.init();
	
	cin >> N;
	int p;
	for(int i = 1; i <= N; i++){
		cin >> p;
		if(i%2) odd.update(i, make_pair(p, i));
		else even.update(i, make_pair(p, i));
	}
	
	Range rg;
	Q.push(Range(1, N));
	while(Q.size()){
		rg = Q.top(), Q.pop();
		ans.push_back(rg.p1.first);
		ans.push_back(rg.p2.first);
		if(rg.p1.second != rg.l) Q.push(Range(rg.l, rg.p1.second-1));
		if(rg.p2.second != rg.r) Q.push(Range(rg.p2.second+1, rg.r));
		if(rg.p1.second+1 != rg.p2.second) Q.push(Range(rg.p1.second+1, rg.p2.second-1));
	}
	
	for(int i = 0; i < ans.size(); i++){
		cout << ans[i];
		if(i != ans.size()-1) cout << " ";
	}
	cout << endl;
	
	return 0;
}

Submission Info

Submission Time
Task E - Young Maids
User leaf1415
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2054 Byte
Status AC
Exec Time 160 ms
Memory 12468 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 23
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt
Case Name Status Exec Time Memory
0_00.txt AC 5 ms 8448 KB
0_01.txt AC 5 ms 8448 KB
0_02.txt AC 5 ms 8448 KB
1_00.txt AC 5 ms 8448 KB
1_01.txt AC 5 ms 8448 KB
1_02.txt AC 136 ms 10612 KB
1_03.txt AC 140 ms 10612 KB
1_04.txt AC 144 ms 10612 KB
1_05.txt AC 156 ms 12340 KB
1_06.txt AC 157 ms 11404 KB
1_07.txt AC 155 ms 12468 KB
1_08.txt AC 159 ms 11432 KB
1_09.txt AC 139 ms 10612 KB
1_10.txt AC 147 ms 10612 KB
1_11.txt AC 139 ms 10612 KB
1_12.txt AC 154 ms 11700 KB
1_13.txt AC 159 ms 11700 KB
1_14.txt AC 160 ms 11700 KB
1_15.txt AC 159 ms 11700 KB
1_16.txt AC 157 ms 11700 KB
1_17.txt AC 157 ms 11700 KB
1_18.txt AC 157 ms 11700 KB
1_19.txt AC 158 ms 11700 KB