zoukankan      html  css  js  c++  java
  • 【UOJ #280】【UTR #2】题目难度提升

    http://uoj.ac/problem/280
    非常难想的贪心,用set(O(nlogn))
    调了一天qwq。
    题解

    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 100003;
    
    multiset <int> S;
    multiset <int> ans;
    multiset <int> :: iterator L;
    multiset <int> :: iterator R;
    
    int a[N], n, hh, m;
    
    int main() {
    	scanf("%d", &n);
    	for (int i = 1; i <= n; ++i) {
    		scanf("%d", a + i); a[i] <<= 1;
    		S.insert(a[i]);
    	}
    	stable_sort(a + 1, a + n + 1);
    	int tmp;
    	for (tmp = (n + 1) / 2; tmp >= 1; --tmp)
    		if (a[tmp] == a[tmp + 1])
    			break;
    	
    	set <int> :: iterator h;
    	if (tmp == 0) {
    		S.erase(a[1]); ans.insert(a[1]); m = a[1];
    		printf("%d ", a[1] >> 1);
    		for (int i = 2; i <= n; ++i) {
    			L = S.begin();
    			h = ans.upper_bound(m);
    			if (h != ans.end() && (*h < *L || *h <= 2 * (*L) - m)) L = --S.end();
    			else L = --S.upper_bound(2 * (*L) - m);
    			printf("%d ", *L >> 1);
    			
    			ans.insert(*L);
    			if (!ans.count(m)) {
    				if (*L > m)	m = *ans.upper_bound(m);
    				else m = *ans.lower_bound(m) - 1;
    			} else if (*L != m) {
    				if (*L > m) hh = *ans.upper_bound(m);
    				else hh = *--ans.lower_bound(m);
    				m = (1ll + m + hh) / 2;
    			}
    			S.erase(*L);
    		}
    	} else {
    		m = a[tmp];
    		S.erase(S.find(m)); S.erase(S.find(m));
    		ans.insert(m); ans.insert(m);
    		printf("%d %d ", m >> 1, m >> 1);
    		tmp = 2; bool flag = false;
    		while (S.size() > 1) {
    			L = --S.end();;
    			if (*L < m) {flag = true; break;}
    			ans.insert(*L); printf("%d ", *L >> 1);
    			S.erase(L);
    			L = S.upper_bound(m);
    			++tmp;
    			if (L == S.begin()) break;
    			--L;
    			ans.insert(*L); printf("%d ", *L >> 1);
    			S.erase(L);
    			++tmp;
    		}
    		
    		if (flag) {
    			while (S.size()) {
    				L = --S.end();;
    				printf("%d ", *L >> 1);
    				S.erase(L);
    			}
    			return 0;
    		}
    		
    		tmp = n - tmp;
    		for (int i = 1; i <= tmp; ++i) {
    			L = S.begin();
    			h = ans.upper_bound(m);
    			if (h != ans.end() && (*h < *L || *h <= 2 * (*L) - m)) L = --S.end();
    			else L = --S.upper_bound(2 * (*L) - m);
    			printf("%d ", *L >> 1);
    			
    			ans.insert(*L);
    			if (!ans.count(m)) {
    				if (*L > m)	m = *ans.upper_bound(m);
    				else m = *ans.lower_bound(m) - 1;
    			} else if (*L != m) {
    				if (*L > m) hh = *ans.upper_bound(m);
    				else hh = *--ans.lower_bound(m);
    				m = (1ll + m + hh) / 2;
    			}
    			S.erase(L);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    OpenCV基本函数学习
    OpenCV:概述、结构和内容
    NS2典型例子简单分析
    NS2仿真过程中需要的语言及基本组件
    洛谷 P2393 yyy loves Maths II
    洛谷 P1922 女仆咖啡厅桌游吧
    洛谷 P2559 [AHOI2002]哈利·波特与魔法石
    洛谷 P1301 魔鬼之城
    洛谷 P2383 狗哥玩木棒
    洛谷 P2298 Mzc和男家丁的游戏
  • 原文地址:https://www.cnblogs.com/abclzr/p/6285944.html
Copyright © 2011-2022 走看看