제출 #1171943

#제출 시각아이디문제언어결과실행 시간메모리
1171943nuutsnoynton중앙값 배열 (balkan11_medians)C++20
0 / 100
52 ms31560 KiB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const ll N = 2e5 + 2;
set < ll > uragsh_ind[N], aragsh_ind[N];
ll uragsh[N], aragsh[N], a[N], b[N], used[N];
int main() {
	ll n, m, r, x,s, y, i, j,ind, ans, t;
	
	cin >> n;
	
	
	
	for (i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	used[a[1]] = 1;
	for (i = 1; i <= 2 * n - 1; i ++) b[i]= -1;
	b[1] = a[1];
	for (i = 2; i <= n; i ++) {
		if ( a[i] == a[i - 1]) {
			uragsh[a[i]] = aragsh[a[i]] = 1;
			uragsh_ind[a[i]].insert(i);
			aragsh_ind[a[i]].insert(i);
		}
		else {
			if ( a[i] > a[i- 1]) {
				aragsh_ind[a[i]].insert(i);
				aragsh[a[i]] =1;
				used[a[i]] = 1;
				b[2 * i - 1] = a[i];
			}
			else {
				uragsh_ind[a[i]].insert(i);
				used[a[i]] = 1;
				uragsh[a[i]] = 1;
				b[2 * i - 1] = a[i];
			}
		}
	}
	queue < ll > q;
	for (i =1 ; i <= 2 *n - 1; i ++) {
		for ( ll ind : uragsh_ind[i]) {
			s = q.front();
			q.pop();
			b[ind * 2 - 2] = s;
		}	
		if ( uragsh_ind[i].empty() && aragsh_ind[i].empty())q.push(i);
	}
	stack < ll > v;
	while(!q.empty()) {
		v.push(q.front());
		q.pop();
	}
	for (i = 2 * n - 1; i >= 1; i --) {
		for ( ll ind : aragsh_ind[i]) {
			s = v.top();
			v.pop();
			//ind =-ind;
			if ( b[ind * 2 - 1] == -1) b[ind * 2 - 1] = s;
			else b[ind * 2 - 2] = s;
		}
	}
//	for (i = 1; i <= 2 * n - 1; i ++) {
//		for (j = i + 1; j <= 2 * n - 1; j ++) {
//			if ( b[i] == b[j]) {
//				while(1);
//			}
//		}
//	}
	for (i = 1; i <= 2 *n - 1; i ++) {
		if ( b[i] < 1 || b[i] > 2 * n - 1) {
			while(1);
		}
		cout << i << " ";
	}
	cout <<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...