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

最新文章

  1. 令人崩溃的@requestBody乱码一例
  2. [MVC_Json序列化]Json字符串反序列化成C#对象
  3. js控制全屏窗口
  4. 【原】xcode5.0升级5.1遇到的clang: error: unknown argument: &#39;-fobj-arc&#39;错误
  5. MyEclipse破解方法
  6. 错误:The request sent by the client was syntactically incorrect的解决
  7. FB面经Prepare: Merge K sorted Array
  8. 宏开发:excel中添加拼接行
  9. Java中栈的应用,括号匹配
  10. React Native小白入门学习路径——四
  11. 深入理解AsyncTask的工作原理
  12. web开发必备的浏览器常识
  13. .Net Core Base64加密解密
  14. 关于 No buffer space available (maximum connections reached?): connect 的处理
  15. scrapy 元素的相对xpath
  16. OO模式-Singleton
  17. 20145326《Java程序设计》实验二Java面向对象程序设计实验报告
  18. 20145314郑凯杰 《Java程序设计》实验三 敏捷开发与XP实践实验报告
  19. 【扫描线】Gym - 101190E - Expect to Wait
  20. [MySQL] 变量(参数)的查看和设置 [转]

热门文章

  1. 使用qt+ros调用摄像头遇到的问题
  2. Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property &#39;URIType&#39; of bean class
  3. HDU - 4990 Reading comprehension 【矩阵快速幂】
  4. ES 相似度算法设置(续)
  5. Jmeter-配置原件-HTTP Cookie管理器
  6. 集训Day9
  7. ACM学习历程—HDU5410 CRB and His Birthday(动态规划)
  8. POJ(有向图求LCA)
  9. DataWindow.Net V2.5原始文件下载
  10. LeafLet之气泡框隐藏&quot;x&quot;图标