bzoj4759 [Usaco2017 Jan]Balanced Photo

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4759

【题解】

排序,从大到小插入,树状数组统计。

# include <vector>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e5 + 10, N = 4e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

# define lb(x) (x&(-x))
struct BIT {
    int c[N], n;
    inline void set(int _n) {
        memset(c, 0, sizeof c);
        n = _n;
    }
    inline void edt(int x, int d) {
        for (; x<=n; x+=lb(x)) c[x] += d;
    }
    inline int sum(int x) {
        int ret = 0;
        for (; x; x-=lb(x)) ret += c[x];
        return ret;
    }
    inline int sum(int x, int y) {
        if(x>y) return 0;
        return sum(y) - sum(x-1);
    }
}T;

int n;
vector<int> ps;
struct pa {
    int x, pos;
    pa() {}
    pa(int x, int pos) : x(x), pos(pos) {}
    friend bool operator < (pa a, pa b) {
        return a.x > b.x;
    }
}p[M];

int L[M], R[M];

int main() {
    cin >> n;
    T.set(n);
    for (int i=1; i<=n; ++i) {
        scanf("%d", &p[i].x);
        p[i].pos = i;
        ps.push_back(p[i].x);
    }
    
    sort(ps.begin(), ps.end());    
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
    
    for (int i=1; i<=n; ++i) p[i].x = lower_bound(ps.begin(), ps.end(), p[i].x) - ps.begin() + 1;
    
    sort(p+1, p+n+1);
    
    for (int i=1; i<=n; ++i) {
        int j = i;
        while(j<n && p[j+1].x == p[i].x) ++j;
        for (int k=i; k<=j; ++k) {
            L[k] = T.sum(1, p[k].pos-1);
            R[k] = T.sum(p[k].pos+1, n);
        }
        for (int k=i; k<=j; ++k) T.edt(p[k].pos, 1);
        i = j;
    }
    
    int ans = 0;
    for (int i=1; i<=n; ++i)
        if(max(R[i], L[i]) > 2*min(R[i], L[i]))
            ++ans;
    
    cout << ans << endl;
    
    return 0;
}
View Code

 

posted @ 2017-05-26 20:16  Galaxies  阅读(211)  评论(0编辑  收藏  举报