洛谷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 ...
随机推荐
- Java基础教程:面向对象编程[2]
Java基础教程:面向对象编程[2] 内容大纲 访问修饰符 四种访问修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default ...
- Spark与缓存
预期成果 1.1 当前问题 当前以图搜图应用存在的问题: 当前使用spark RDD方案无法达到数据实时加载(每10分钟加载一次,虽然可配,但太短可能会有问题) Spark RDD内存会被分为两部 ...
- Database: index
The whole point of having an index is to speed up search queries by essentially cutting down the num ...
- absolute布局的替代实现
京东商城首页标价定位 小横条首页导航的下拉 1.京东商城首页标价定位 .p-img{ height: 130px;} .p-price{ margin:-28px 0 0 74px;} .price{ ...
- 网页布局的应用(float或absolute)
一个浮动(左浮动或右浮动) 垂直环绕布局(float.clear) 左右两列布局(float.absolute) 三栏网页宽度自适应布局(float.absolute) 注意:网页设计中应该尽量避免使 ...
- ES6中的class 与prototype
一.定义构造函数 在以前的js中,生成一个对象实例,需要先定义构造函数,然后通过prototype 的方式来添加方法,在生成实例: function Person(){ this.name = &qu ...
- hdu 2015校赛1002 Dual horsetail (思维题 )
Dual horsetail Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- FZU1989 AntiAC —— 字符串
题目链接:https://vjudge.net/problem/FZU-1989 Problem 1989 AntiAC Accept: 79 Submit: 399Time Limit: 4 ...
- Spring Boot2.0之@Async实现异步调用
补充一个知识点: lombok底层原理使用的是: 字节码技术ASM修改字节码文件,生成比如类似于get() set( )方法 一定要在开发工具安装 在编译时候修改字节码文件(底层使用字节码技术),线上 ...
- BZOJ 1637 [Usaco2007 Mar]Balanced Lineup:前缀和 + 差分
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1637 题意: Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ...