4759: [Usaco2017 Jan]Balanced Photo

4759: [Usaco2017 Jan]Balanced Photo

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 103 Solved: 83
[Submit][Status][Discuss]
Description

Farmer John is arranging his N cows in a line to take a photo (1≤N≤100,000). The height of the iit
h cow in sequence is hihi, and the heights of all cows are distinct.As with all photographs of his c
ows, FJ wants this one to come out looking as nice as possible. He decides that cow ii looks “unbala
nced” if Li and RRi differ by more than factor of 2, where LiLi and RiRi are the number of cows tall
er than i on her left and right, respectively. That is, ii is unbalanced if the larger of Li and Ri
is strictly more than twice the smaller of these two numbers. FJ is hoping that not too many of his
cows are unbalanced.Please help FJ compute the total number of unbalanced cows.
农夫约翰正在安排他的N头牛拍照片, 每头牛有一个身高,从1到N编号,排列成一行(h1,h2…hn),每头牛i左边
比他高的牛的数量记为Li,右边比他高的牛的数量记为Ri,如果存在i满足max(Ri,Li)>2*min(Li,Ri)则这个牛i是
不平衡的,现在FJ需要你告诉他有多少头牛不平衡。
Input

The first line of input contains N. The next NN lines contain h1…hN
each a nonnegative integer at most 1,000,000,000.
输入第一行为N(N<=1e5),接下来的一行有N个数,每个数表示第i头牛的身高,不超过1e9
Output

Please output a count of the number of cows that are unbalanced.
输出有多少头牛是不平衡的
Sample Input

7

34

6

23

0

5

99

2
Sample Output

3

//在这个样例中,身高为34,5,2的牛是不平衡的
HINT

Source

Gold

[Submit][Status][Discuss]

usaco划水。。。
按权值排序完以后就可以树状数组大力统计啦。。。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

const int maxn = 1E5 + 10;
typedef pair <int,int> pr;

#define fr first
#define sc second
#define mp(a,b) (make_pair((a),(b)))

int n,Ans,c[maxn];

pr p[maxn];

inline int getint()
{
    char ch = getchar(); int ret = 0;
    while (ch < '0' || '9' < ch) ch = getchar();
    while ('0' <= ch && ch <= '9')
        ret = ret * 10 + ch - '0',ch = getchar();
    return ret;
}

int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif

    n = getint();
    for (int i = 1; i <= n; i++) p[i] = mp(getint(),i);
    sort(p + 1,p + n + 1);
    for (int i = n; i; i--)
    {
        int A = 0,B = 0;
        for (int j = n; j > 0; j -= j&-j) B += c[j];
        for (int j = p[i].sc; j > 0; j -= j&-j) A += c[j];
        B -= A; if (A > B) swap(A,B);
        if ((A << 1) < B) ++Ans;
        for (int j = p[i].sc; j <= n; j += j&-j) ++c[j];
    }
    cout << Ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值