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;
}

时间: 2024-10-11 21:22:10

bzoj4759 [Usaco2017 Jan]Balanced Photo的相关文章

bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的

[BZOJ] 1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 909  Solved: 644[Submit][Status][Discuss] Description For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer

[bzoj 1699] [Usaco2007 Jan]Balanced Lineup排队

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1699 [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1443  Solved: 920[Submit][Status][Discuss] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一

BZOJ1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[Submit][Status] Description For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer John deci

洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a photo (1 \leq N \leq 100,0001≤N≤100,000). The height of the iith cow in sequence is h_ih?i??, and the heights of all cows are distinct. As with all ph

BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 568[Submit][Status] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. Jo

[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: 217[Submit][Status][Discuss] Description The cows have once again tried to form a startup company, failing to remember from past experience t hat cow

[Usaco2007 Jan]Balanced Lineup排队

[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q &l

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差,显然直接上线段树或者rmq维护区间最值即可. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50010 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #def