zoukankan      html  css  js  c++  java
  • [APIO2015]巴邻旁之桥

    Bzoj权限题
    luogu题面

    先去掉同边的
    首先k==1,即求一个点j
    使(sum_{iin A} |D_i - D_j| + sum_{iin B} |D_i - D_j|)最小
    因为两边j是一样的,直接合在一起就好
    所以就是(sum |D_i - D_j|)最小
    那么j就是的中位数,合在一起排序就好了

    然后k==2,
    设每个人的两个位置为A,B那么他肯定去离(frac{A+B}{2})最近的桥
    所以可以先把这些人按(frac{A+B}{2})排序,再枚举这些人从哪里分开,左右单独考虑取中位数就好,最后取min

    也就是说现在要动态维护区间中位数,可以用线段树或者平衡树维护size,排序后插入,树上二分找中位数,比较左右size即可

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    # define Copy(a, b) memcpy(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(2e5 + 10), __(1e6 + 10);
    
    IL ll Read(){
        RG char c = getchar(); RG ll x = 0, z = 1;
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int k, n, cnt, o[_], len, sz[2][__];
    struct Zsy{
    	int a, b;
    	IL bool operator <(RG Zsy Orz) const{  return a + b < Orz.a + Orz.b;  }
    } p[_];
    ll ans, sum[2][__];
    
    IL void Modify(RG int c, RG int x, RG int l, RG int r, RG int v, RG int op){
    	sz[c][x] += op; sum[c][x] += op * o[v];
    	if(l == r) return;
    	RG int mid = (l + r) >> 1;
    	if(v <= mid) Modify(c, x << 1, l, mid, v, op);
    	else Modify(c, x << 1 | 1, mid + 1, r, v, op);
    }
    
    IL int Find(RG int c, RG int x, RG int l, RG int r, RG int k){
    	if(l == r) return l;
    	RG int mid = (l + r) >> 1;
    	if(k <= sz[c][x << 1]) return Find(c, x << 1, l, mid, k);
    	return Find(c, x << 1 | 1, mid + 1, r, k - sz[c][x << 1]);
    }
    
    IL ll Query(RG int c, RG int x, RG int l, RG int r, RG int L, RG int R, RG int op){
    	if(L <= l && R >= r) return op ? sum[c][x] : sz[c][x];
    	RG int mid = (l + r) >> 1; RG ll yyb = 0;
    	if(L <= mid) yyb = Query(c, x << 1, l, mid, L, R, op);
    	if(R > mid) yyb += Query(c, x << 1 | 1, mid + 1, r, L, R, op);
    	return yyb;
    }
    
    int main(RG int argc, RG char* argv[]){
    	k = Read(); n = Read();
    	if(k == 1){
    		for(RG int i = 1; i <= n; ++i){
    			RG char c1, c2; RG int ss, tt;
    			scanf(" %c", &c1); ss = Read();
    			scanf(" %c", &c2); tt = Read();
    			if(c1 == c2) ans += abs(ss - tt);
    			else o[++cnt] = ss, o[++cnt] = tt;
    		}
    		RG int mid = cnt >> 1; ans += mid;
    		sort(o + 1, o + cnt + 1);
    		for(RG int i = 1; i <= cnt; ++i) ans += abs(o[i] - o[mid]);
    		printf("%lld
    ", ans);
    	}
    	else{
    		RG ll ret = 0; ans = 1e18;
    		for(RG int i = 1; i <= n; ++i){
    			RG char c1, c2; RG int ss, tt;
    			scanf(" %c", &c1); ss = Read();
    			scanf(" %c", &c2); tt = Read();
    			if(c1 == c2) ret += abs(ss - tt);
    			else p[++cnt].a = ss, p[cnt].b = tt, o[++len] = ss, o[++len] = tt;
    		}
    		ret += cnt;	sort(p + 1, p + cnt + 1);
    		sort(o + 1, o + len + 1); len = unique(o + 1, o + len + 1) - o - 1;
    		RG ll ss1 = 0, ss2 = 0;
    		for(RG int i = 1; i <= cnt; ++i){
    			ss2 += p[i].a + p[i].b;
    			p[i].a = lower_bound(o + 1, o + len + 1, p[i].a) - o;
    			p[i].b = lower_bound(o + 1, o + len + 1, p[i].b) - o;
    			Modify(1, 1, 1, len, p[i].a, 1); Modify(1, 1, 1, len, p[i].b, 1);
    		}
    		for(RG int i = 1; i <= cnt; ++i){
    			ss1 += o[p[i].a] + o[p[i].b]; ss2 -= o[p[i].a] + o[p[i].b];
    			Modify(0, 1, 1, len, p[i].a, 1); Modify(0, 1, 1, len, p[i].b, 1);
    			Modify(1, 1, 1, len, p[i].a, -1); Modify(1, 1, 1, len, p[i].b, -1);
    			RG int mid1 = Find(0, 1, 1, len, i), mid2 = Find(1, 1, 1, len, cnt - i);
    			RG ll s1 = Query(0, 1, 1, len, 1, mid1, 1), s2 = Query(1, 1, 1, len, 1, mid2, 1);
    			RG ll z1 = Query(0, 1, 1, len, 1, mid1, 0), z2 = Query(1, 1, 1, len, 1, mid2, 0);
    			RG ll tot1 = z1 * o[mid1] - s1 + (ss1 - s1) - (2 * i - z1) * o[mid1];
    			RG ll tot2 = z2 * o[mid2] - s2 + (ss2 - s2) - (2 * (cnt - i) - z2) * o[mid2];
    			ans = min(ans, tot1 + tot2 + ret);
    		}
    		printf("%lld
    ", cnt ? ans : ret);
    	}
        return 0;
    }
    
    
  • 相关阅读:
    360安全桌面的屏保图片在哪个目录下?
    IntelliJ 注解@Slf4j后找不到log问题解决
    运行springboot项目报错:Field userMapper in XX required a bean of type 'xx' that could not be found.
    springsecurity
    Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring/applicationContext.xml]: Invocation of init method failed;
    idea热部署
    数据结构导论 三 线性表-顺序存储
    数据结构导论 二 时空性
    树莓派安装、卸载、更新软件
    数据结构导论第一章
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8214424.html
Copyright © 2011-2022 走看看