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 photographs of his cows, FJ wants this one to come out looking as nice as possible. He decides that cow ii looks "unbalanced" if L_iL​i​​ and R_iR​i​​ differ by more than factor of 2, where L_iL​i​​ and R_iR​i​​ are the number of cows taller than ii on her left and right, respectively. That is, ii is unbalanced if the larger of L_iL​i​​ and R_iR​i​​ 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.

FJ正在安排他的N头奶牛站成一排来拍照。(1<=N<=100,000)序列中的第i头奶牛的高度是h[i],且序列中所有的奶牛的身高都不同。

就像他的所有牛的照片一样,FJ希望这张照片看上去尽可能好。他认为,如果L[i]和R[i]的数目相差2倍以上的话,第i头奶牛就是不平衡的。(L[i]和R[i]分别代表第i头奶牛左右两边比她高的数量)。如果L[i]和R[i]中较大者比较小者的数量严格多两倍的话,这头奶牛也是不平衡的。FJ不希望他有太多的奶牛不平衡。

请帮助FJ计算不平衡的奶牛数量。

输入输出格式

输入格式:

The first line of input contains NN. The next NN lines contain h_1 \ldots h_Nh​1​​…h​N​​, each a nonnegative integer at most 1,000,000,000.

第一行一个整数N。接下N行包括H[1]到H[n],每行一个非负整数(不大于1,000,000,000)。

输出格式:

Please output a count of the number of cows that are unbalanced.

请输出不平衡的奶牛数量。

输入输出样例

输入样例#1:

7
34
6
23
0
5
99
2
输出样例#1:

3
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=;
int n,a[maxn],b[maxn];
int l[maxn],r[maxn],f[maxn];
inline int lowbit(int x){
return x&-x;
}
inline void add(int x){
for(;x<=n;x+=lowbit(x))
f[x]++;
}
inline int sum(int x){
int ans=;
for(;x;x-=lowbit(x))
ans+=f[x];
return ans;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b+n+,a[i])-b;
for(int i=;i<=n;i++)
l[i]=i--sum(a[i]),add(a[i]);//正着找逆序对
memset(f,,sizeof(f));
for(int i=n;i;i--)
r[i]=n-i-sum(a[i]),add(a[i]);//倒着找正序对
int ans=;
for(int i=;i<=n;i++){
if(l[i]==&&r[i]==) continue;
if(!l[i]||!r[i]) ans++;
else{
int xx=max(l[i],r[i]),yy=min(l[i],r[i]);
if(xx/yy>) ans++;
else if(xx/yy==&&yy*!=xx) ans++;
}
}
printf("%d",ans);
return ;
}
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=;
int n,a[maxn],b[maxn];
int l[maxn],r[maxn],f[maxn];
inline int lowbit(int x){
return x&-x;
}
inline void add(int x){
for(;x<=n;x+=lowbit(x))
f[x]++;
}
inline int sum(int x){
int ans=;
for(;x;x-=lowbit(x))
ans+=f[x];
return ans;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b+n+,a[i])-b;
for(int i=;i<=n;i++)
l[i]=i--sum(a[i]),add(a[i]);//正着找逆序对
memset(f,,sizeof(f));
for(int i=n;i;i--)
r[i]=n-i-sum(a[i]),add(a[i]);//倒着找正序对
int ans=;
for(int i=;i<=n;i++){
if(l[i]==&&r[i]==) continue;
if(!l[i]||!r[i]) ans++;
else{
int xx=max(l[i],r[i]),yy=min(l[i],r[i]);
if(xx/yy>) ans++;
else if(xx/yy==&&yy*!=xx) ans++;
}
}
printf("%d",ans);
return ;
}

洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片的更多相关文章

  1. 【luogu P3608 [USACO17JAN]Balanced Photo平衡的照片】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3608 乍一看很容易想到O(N^2)的暴力. 对于每个H[i]从i~i-1找L[i]再从i+1~n找R[i], ...

  2. [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)

    传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...

  3. [USACO17JAN]Balanced Photo平衡的照片 (树状数组)

    题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...

  4. POJ 3274/洛谷 1360:Gold Balanced Lineup 黄金阵容平衡

    题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to na ...

  5. 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…

    P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...

  6. 【题解】[USACO17JAN]Balanced Photo G

    题目链接:https://www.luogu.com.cn/problem/P3608 方法一 用树状数组求逆序对先后扫两遍,一次从前往后,一次从后往前,算出每头奶牛左右两边比她高的数量. 最后统计一 ...

  7. 2018.08.16 洛谷P3607 [USACO17JAN]序列反转(线性dp)

    传送门 一道感觉比较简单的dp. 注意是要求翻转一个子序列而不是一段连续的数(被坑了很多次啊)... 看到数据范围果断开一个四维数组来dp一波. 我们显然可以用f[i][j][k][t]表示下标在[l ...

  8. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  9. 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...

随机推荐

  1. java中的clone方法

    Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象.那 ...

  2. mysql服务器启动问题

    The server quit without updating PID file (/usr/local/mysql/data/snsgou.pid);可能的情况是启动的用户不对,

  3. 【Leetcode-easy】Valid Parentheses

    思路:读取字符串中的每一个字符,并与栈顶元素进行比较,如果匹配则栈顶元素出栈,否则进栈.直到全部元素都出栈,否则该括号字符串不合法.需要注意细节. public boolean isValid(Str ...

  4. linux内核中创建线程方法【转】

    本文转载自:https://www.cnblogs.com/Ph-one/p/6077787.html 1.头文件 #include <linux/sched.h> //wake_up_p ...

  5. BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...

  6. 常见的25个python面试问答

    常见的25个python面试问答 说到好用简洁的大数据技术,除了Hadoop.R等等,Python也是其中熠熠生辉的一员,因而广受企业和商家的青睐.求职季,不少应聘者在面试相关职业时都被要求掌握Pyt ...

  7. Jmeter-聚合报告

    线程组右键--添加--监听器--聚合报告 Aggreagete Report:jmeter最常用的一个Listener,“聚合报告”. Label:每个jmeter的element(例如HTTP Re ...

  8. phpstorm 代码按列对齐

    设置方式: Preference... -> Editor -> CodeStyle -> PHP -> Other -> Align key-value pairs

  9. liunx让命令窗口显示段路径的方法

    平时我们使用linux终端命令行的时候,常常会被一个问题困扰,那就是文件路径过长,有时候甚至超过了一行,这样看起来非常别扭,其实只要两步就可以解决这个问题: 1,修改.bashrc文件(用户根目录下) ...

  10. myeclipse 2017破解安装教程+开发环境部署(jdk+tomcat)

    点击安装包,进入安装界面,点击next 选择接受协议,点击next 选择安装目录,点击next 格局自己电脑的机型选择32bit或64bit,点击next 安装完成后不要运行MyEclipse,将 & ...