답안 #101219

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101219 2019-03-17T16:53:04 Z mraron 중앙값 배열 (balkan11_medians) C++14
5 / 100
160 ms 11512 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(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]) {
				addmin();
				addmin();
			}else {
				addmax();
				addmax();
			}
		}else {
			s.erase(t[i]);
			fw.incr(t[i], 1);
			ans.push_back(t[i]);
			if(med()<t[i]) addmin();
			else addmax();
		}
	}
	
	for(auto i:ans) {
		cout<<i<<" ";
	}
	cout<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1152 KB Output isn't correct
2 Incorrect 3 ms 1152 KB Output isn't correct
3 Incorrect 3 ms 1152 KB Output isn't correct
4 Incorrect 3 ms 1152 KB Output isn't correct
5 Incorrect 3 ms 1152 KB Output isn't correct
6 Correct 4 ms 1152 KB Output is correct
7 Incorrect 4 ms 1280 KB Output isn't correct
8 Incorrect 4 ms 1152 KB Output isn't correct
9 Incorrect 3 ms 1152 KB Output isn't correct
10 Incorrect 3 ms 1152 KB Output isn't correct
11 Incorrect 3 ms 1152 KB Output isn't correct
12 Incorrect 3 ms 1152 KB Output isn't correct
13 Incorrect 5 ms 1280 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 1536 KB Output isn't correct
2 Incorrect 8 ms 1536 KB Output isn't correct
3 Incorrect 14 ms 2048 KB Output isn't correct
4 Incorrect 20 ms 2944 KB Output isn't correct
5 Incorrect 42 ms 4352 KB Output isn't correct
6 Incorrect 96 ms 7808 KB Output isn't correct
7 Incorrect 160 ms 11512 KB Output isn't correct