洛谷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_ihi, 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_iLi and R_iRi differ by more than factor of 2, where L_iLi and R_iRi are the number of cows taller than ii on her left and right, respectively. That is, ii is unbalanced if the larger of L_iLi and R_iRi 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_Nh1…hN, 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.
请输出不平衡的奶牛数量。
输入输出样例
7
34
6
23
0
5
99
2
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平衡的照片的更多相关文章
- 【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], ...
- [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)
传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...
- [USACO17JAN]Balanced Photo平衡的照片 (树状数组)
题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...
- 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 ...
- 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…
P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...
- 【题解】[USACO17JAN]Balanced Photo G
题目链接:https://www.luogu.com.cn/problem/P3608 方法一 用树状数组求逆序对先后扫两遍,一次从前往后,一次从后往前,算出每头奶牛左右两边比她高的数量. 最后统计一 ...
- 2018.08.16 洛谷P3607 [USACO17JAN]序列反转(线性dp)
传送门 一道感觉比较简单的dp. 注意是要求翻转一个子序列而不是一段连续的数(被坑了很多次啊)... 看到数据范围果断开一个四维数组来dp一波. 我们显然可以用f[i][j][k][t]表示下标在[l ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈
题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...
随机推荐
- 剑指Offer:二叉树打印成多行【23】
剑指Offer:二叉树打印成多行[23] 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目分析 Java题解 package tree; import java.uti ...
- client , offset , scroll 系列 及百度导航栏案例
1. client 系列 示例 : <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- nginx语法之location详解
Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...
- linux shell发送邮件
我的系统环境: [root@NPS-JK ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6.1 (Santiago) Kerne ...
- ssh_jar包选择
1.struts2.3.29 + spring-framework-3.2.9.RELEASE +hibernate-distribution-3.6.10 * struts 所需jar 在:如下所示 ...
- glViewport()函数和glOrtho()函数的理解(转)
转:http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html 摘要:glOrtho相当指定图框的大小,由此会使得图框里的图形 ...
- 分享知识-快乐自己:Hibernate 中Criteria Query查询详解
1):Hibernate 中Criteria Query查询详解 当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中. 此外,Hibernate还支持Cr ...
- CentOS 7编译安装Tengine+PHP+MariaDB全程笔记
安装环境:CentOS7 3.10.0-693.5.2.el7.x86_64 准备源码包: pcre-8.41.tar.gz openssl-1.0.1h.tar.gz zlib-1.2.11.tar ...
- 事件驱动模式--Reactor
原文:https://www.cnblogs.com/harvyxu/p/7498763.html 1 Reactor模型 Reactor模式是处理并发I/O比较常见的一种模式,用于同步I/O,中心思 ...
- linux网络编程 gethostbyname()
gethostbyname()返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针.结构的声明与gethostaddr()中一致. 返回对应于给定主机名的主机信息. #include ...