zoukankan      html  css  js  c++  java
  • loj2472 「九省联考 2018」IIIDX

    ref

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int n, a[500005], fa[500005], siz[500005], cnt[500005], ans[500005];
    int hmn[500005];
    double k;
    bool vis[500005];
    struct SGT{
    	int val[2000005], tag[2000005];
    	void build(int o, int l, int r){
    		if(l==r)	val[o] = l;
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			if(l<=mid)	build(lson, l, mid);
    			if(mid<r)	build(rson, mid+1, r);
    			val[o] = min(val[lson], val[rson]);
    		}
    	}
    	void pushDown(int o, int lson, int rson){
    		tag[lson] += tag[o];
    		tag[rson] += tag[o];
    		val[lson] += tag[o];
    		val[rson] += tag[o];
    		tag[o] = 0;
    	}
    	void update(int o, int l, int r, int x, int y, int k){
    		if(l>=x && r<=y){
    			val[o] += k;
    			tag[o] += k;
    		}
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			if(tag[o])	pushDown(o, lson, rson);
    			if(x<=mid)	update(lson, l, mid, x, y, k);
    			if(mid<y)	update(rson, mid+1, r, x, y, k);
    			val[o] = min(val[lson], val[rson]);
    		}
    	}
    	int query(int o, int l, int r, int x){
    		if(l==r)	return val[o]>=x?l:l+1;
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			if(tag[o])	pushDown(o, lson, rson);
    			if(val[rson]>=x)	return query(lson, l, mid, x);
    			else	return query(rson, mid+1, r, x);
    		}
    	}
    }sgt;
    bool cmp(int a, int b){
    	return a>b;
    }
    int main(){
    	cin>>n>>k;
    	for(int i=1; i<=n; i++){
    		siz[i] = 1;
    		fa[i] = i / k;
    		scanf("%d", &a[i]);
    	}
    	sort(a+1, a+1+n, cmp);
    	for(int i=n; i; i--){
    		siz[fa[i]] += siz[i];
    		if(a[i]!=a[i+1])	cnt[i] = 0;
    		else	cnt[i] = cnt[i+1] + 1;
    	}
    	sgt.build(1, 1, n);
    	for(int i=1; i<=n; i++){
    		if(fa[i] && !vis[fa[i]]){
    			sgt.update(1, 1, n, ans[fa[i]], n, siz[fa[i]]-1);
    			vis[fa[i]] = true;
    		}
    		int x=sgt.query(1, 1, n, siz[i]);
    		x += cnt[x]; hmn[x]++; x -= hmn[x] - 1;
    		ans[i] = x;
    		sgt.update(1, 1, n, x, n, -siz[i]);
    	}
    	for(int i=1; i<=n; i++)
    		printf("%d ", a[ans[i]]);
    	printf("
    ");
    	return 0;
    }
    
  • 相关阅读:
    动态规划----背包问题
    动态规划----最长公共子序列
    贪心算法---codeforce680D Bear and Tower of Cubes
    贪心算法----Fence Repair(POJ 3253)
    线段树拓展----HDU 6315 Naive Operations
    Saruman`s Army(POJ 3069)
    字典序最小问题 Best Cow Line (POJ 3617)
    RMQ
    牛客2018年第二次acm暑期培训----H-Diff-prime Pairs
    牛客2018年第二次多校培训----A-run
  • 原文地址:https://www.cnblogs.com/poorpool/p/9095077.html
Copyright © 2011-2022 走看看