답안 #101222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101222 2019-03-17T17:15:21 Z mraron 중앙값 배열 (balkan11_medians) C++14
100 / 100
189 ms 10956 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN=200101;
struct fenwick {
	int arr[MAXN];
	
	fenwick() {memset(arr,0,sizeof arr);}
	
	void incr(int x, int by) {
		for(;x<MAXN;x+=(x&(-x))) {
			arr[x]+=by;
		}
	} 
	
	int sum(int x) {
		int sum=0;
		for(;x>0;x-=(x&(-x))) {
			sum+=arr[x];
		}
		
		return sum;
	}
};




int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	cin>>n;
	vector<int> t(n);
	for(int i=0;i<n;++i) cin>>t[i];
	set<int> s;
	for(int i=1;i<=2*n-1;++i) s.insert(i);
	fenwick fw;
	fw.incr(t[0], 1);
	s.erase(t[0]);
	vector<int> ans;
	ans.push_back(t[0]);
	
	auto med=[&]() -> int {
		int all=fw.sum(MAXN-1);
		int kell=(all+1)/2;
		int akt=MAXN-1;
		for(int i=20;i>=0;i--) {
			int curr=akt-(1<<i);
			if(curr>=1) {
				if(all%2==1) {
					if(fw.sum(curr)>=kell) {
						akt=curr;
					}
				}else {
					if(fw.sum(curr)>kell) {
						akt=curr;
					}
				}
			}
		}
		
		return akt;
	};
	
	auto addmin=[&](){
		int val=*s.begin();
		s.erase(val);
		fw.incr(val, 1);
		ans.push_back(val);
	};
	
		
	auto addmax=[&](){
		auto it=s.end();
		it--;
		int val=*it;
		s.erase(val);
		fw.incr(val, 1);
		ans.push_back(val);
	};
	
	for(int i=1;i<n;++i) {
		if(s.find(t[i])==s.end()) {
			if(med()<t[i]) {
				addmax();
				addmax();
			}else if(med()>t[i]) {
				addmin();
				addmin();
			}else {
				addmin();
				addmax();
			}
		}else {
			s.erase(t[i]);
			fw.incr(t[i], 1);
			ans.push_back(t[i]);
		//	cerr<<i<<":"<<med()<<"\n";
			if(med()>t[i]) addmin();
			else addmax();
			
		}
	}
	
	for(auto i:ans) {
		cout<<i<<" ";
	}
	cout<<"\n";
	
	/*for(int i=0;i<(int)ans.size();i+=2) {
		vector<int> akt;
		for(int j=0;j<=i;++j) akt.push_back(ans[j]);
		sort(akt.begin(), akt.end());
		cerr<<akt[akt.size()/2]<<" ";
		//for(auto i:akt) cerr<<i<<" ";
		//cerr<<"\n";
	}cerr<<"\n";*/
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1152 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
3 Correct 2 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 3 ms 1152 KB Output is correct
6 Correct 4 ms 1152 KB Output is correct
7 Correct 3 ms 1152 KB Output is correct
8 Correct 3 ms 1152 KB Output is correct
9 Correct 3 ms 1152 KB Output is correct
10 Correct 3 ms 1152 KB Output is correct
11 Correct 3 ms 1152 KB Output is correct
12 Correct 3 ms 1152 KB Output is correct
13 Correct 3 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB Output is correct
2 Correct 6 ms 1536 KB Output is correct
3 Correct 16 ms 2048 KB Output is correct
4 Correct 24 ms 2724 KB Output is correct
5 Correct 50 ms 4324 KB Output is correct
6 Correct 100 ms 7416 KB Output is correct
7 Correct 189 ms 10956 KB Output is correct