BZOJ4411——[Usaco2016 Feb]Load balancing

1、题意:
给出N个平面上的点。保证每一个点的坐标都是正奇数。
你要在平面上画两条线,一条是x=a,一条是y=b,且a和b都是偶数。
直线将平面划成4个部分,要求包含点数最多的那个部分点数最少。
2、分析:我们首先二分答案。。。然后我们枚举横着在哪里切开,用两个树状数组维护上下界,
       保证四个框框都在mid之内。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define M 100010
 
inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
 
struct Node{
    int x, y;
 
    inline bool operator < (const Node& rhs) const{
        return y < rhs.y;
    }
} a[M];
pair<int , int> li[M];
int C[2][M], tt;
int n;
 
inline void change(int c[], int x, int y){
    for(; x <= n; x += (x & -x)) c[x] += y;
}
 
inline int query(int c[], int x){
    int res = 0;
    for(; x > 0; x -= (x & -x)) res += c[x];
    return res;
}
 
inline bool check(int x){
    int size0 = n, size1 = 0;
    memset(C, 0, sizeof(C));
    for(int i = 1; i <= n; i ++) change(C[0], a[i].x, 1);
    int it0 = 1, it1 = n;
    for(int t, j = 1, i = 1; i <= n; i = j){
        while(a[j].y == a[i].y){
            change(C[0], a[j].x, -1), size0 --;
            change(C[1], a[j].x, 1), size1 ++; j ++;
        }
        while(it0 <= n && query(C[0], it0) <= x) it0 ++; it0 --;
        while(it1 > 0 && query(C[1], it1) > x) it1 --;
        t = min(it0, it1);
        if(size0 - query(C[0], t) <= x && size1 - query(C[1], t) <= x) return 1;
    }
    return 0;
}
 
int main(){
    n = read();
    for(int i = 1; i <= n; i ++) a[i].x = read(), a[i].y = read(), li[i].first = a[i].x, li[i].second = i;
    sort(li + 1, li + n + 1);
    tt = 0;
    li[0].first = -2147483647;
    for(int i = 1; i <= n; i ++){
        if(li[i].first != li[i - 1].first) tt ++;
        a[li[i].second].x = tt;
    }
    sort(a + 1, a + n + 1);
    int l = 1, r = n, ans = n;
    while(l <= r){
        int mid = (l + r) / 2;
        if(check(mid)) r = (ans = mid) - 1;
        else l = mid + 1;
    }
    printf("%d\n", ans);
    return 0;
}


你可能感兴趣的:(load,树状数组,二分,Balancing,bzoj,4411)