Submission #1494833


Source Code Expand

#include<bits/stdc++.h>

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define vi vector<int>
typedef long long ll;
#define MAXN 202020
using namespace std;



int n;
vector<vi> t(2 , vi(2 * MAXN , INF));


void build(int par){
    for(int i = n - 1 ; i > 0 ; --i){
        t[par][i] = min(t[par][2 * i] , t[par][2 * i + 1]);
    }
}
 
void modify(int par , int p , int val){
    for(t[par][p += n] = val ; p > 1 ; p /= 2){
        t[par][p / 2] = min(t[par][p] , t[par][p ^ 1]);
    }
}
 
int query(int par , int l , int r){//[l , r)
    int res = INF;
    for(l += n , r += n ; l < r ; l /= 2 , r /= 2){
        if(l % 2){
            res = min(res, t[par][l]);
            ++l;
        }
        if(r % 2){
            --r;
            res = min(res , t[par][r]);
        }
    }
    return res;
}

int last = 0;
map<int , pii> code;
map<pii , int> rcode;
vi pos;

vi g[MAXN / 2];

void solve(vi & p , int l , int r , int prev = -1){
	if(r - l < 1)return;
	if(r >= sz(p) || l < 0)return;
	if(r - l == 1){
		code[last] = {p[l] , p[r]};
		rcode[{p[l] , p[r]}] = last;
		
		if(prev != -1){
			g[prev].pb(last);
		}
		prev = last;
		++last;
	}else{
		pii o = {0 , 0};
		
		int par = l % 2;
		o.fi = query(par , l , r + 1);
		
		int pos1 = pos[o.fi];
		o.se = query((pos1 + 1) % 2 , pos1 + 1 , r + 1);
		
		code[last] = o;
		rcode[o] = last;
		
		if(prev != -1){
			g[prev].pb(last);
		}
		prev = last;
		++last;	

		int pos2 = pos[o.se];

		solve(p , pos1 + 1 , pos2 - 1 , prev);
		solve(p , l , pos1 - 1 , prev);
		solve(p , pos2 + 1 , r  , prev);
		
	}
}

vi restore(){
	vi res;
	priority_queue<pii , vector<pii> , greater<pii>> q; 
	q.push(code[0]);
	
	while(!q.empty()){
		pii u = q.top();
		q.pop();
		
		res.pb(u.fi);
		res.pb(u.se);
		
		for(int c : g[rcode[u]]){
			q.push(code[c]);
		}
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.precision(0); 


	cin >> n;
	
	pos.resize(n + 1);
	
	vi p(n);
	rep(i , n){
		cin >> p[i];
		pos[p[i]] = i;
		
		int par = i % 2;
		
		t[par][n + i] = p[i];
		t[par ^ 1][n + i] = INF;
	}

	
	build(0);
	build(1);
	
	solve(p , 0 , n - 1);
	
	for(int e : restore()){
		cout << e << " ";
	}
	
	return 0;
}

Submission Info

Submission Time
Task E - Young Maids
User ezewin
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2862 Byte
Status AC
Exec Time 174 ms
Memory 38612 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 5844 KB
0_01.txt AC 5 ms 5844 KB
0_02.txt AC 5 ms 5844 KB
1_00.txt AC 5 ms 5844 KB
1_01.txt AC 5 ms 5844 KB
1_02.txt AC 132 ms 38612 KB
1_03.txt AC 127 ms 38612 KB
1_04.txt AC 139 ms 38484 KB
1_05.txt AC 167 ms 24788 KB
1_06.txt AC 171 ms 25172 KB
1_07.txt AC 162 ms 24404 KB
1_08.txt AC 167 ms 25044 KB
1_09.txt AC 125 ms 38484 KB
1_10.txt AC 136 ms 35412 KB
1_11.txt AC 128 ms 34772 KB
1_12.txt AC 158 ms 24276 KB
1_13.txt AC 168 ms 24916 KB
1_14.txt AC 170 ms 24660 KB
1_15.txt AC 166 ms 25044 KB
1_16.txt AC 165 ms 24916 KB
1_17.txt AC 174 ms 24916 KB
1_18.txt AC 167 ms 24916 KB
1_19.txt AC 166 ms 24660 KB