洛谷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 ...
随机推荐
- 扫盲--.net 程序集
前言:用了几天的时间把高级编程里面程序集一章看完了,原来自己只知道写代码,右键添加引用,从来也不知道操作的实质是什么,微软总是这个套路,鼠标点点就能把任务完成,这对新手友好但是对要通透了解程序执行和内 ...
- yum 安装软件时出现 is this ok [y/d/n]
y下载安装 d只下载不安装 n不安装
- gradle 添加依赖
files 添加libs下的 project 添加一个module compile 去下载一个
- Android字符设备驱动开发基于高通msm8916【原创 】
本人才疏浅学,写一篇文档总结自己在msm8916平台上移植自己编写的简单的字符设备驱动开发的整个流程.这个小项目的主要功能是开发一个简单的APP,APP通过JNI去调用位于kernel的字符设备驱动. ...
- bzoj5213: [Zjoi2018]迷宫
好题!话说省选的都开始构造了吗 由于有K的倍数的限制所以不妨取模,先建K个点表示0~K-1这些数,第i个点向[i*m,i*m+m]建边.不难发现这是合法的但不一定是最优的 考虑合并等价的点,首先从直观 ...
- 9.1 NOIP普及组试题精解(1)
9-1 series1.c #include <stdio.h> int main() { float s=0.0,k; int n; printf("输入数字k(1~15):& ...
- Python 使用正则表达式验证密码必须包含大小写字母和数字
校验密码是否合法的程序. 输入一个密码 1.长度5-10位 2.密码里面必须包含,大写字母.小写字母和数字 3.最多输入5次 ===================================== ...
- centos7搭建redis主从复制,并模拟故障切换。
Cntos7搭建redis主从复制,并模拟故障主从切换 主从复制搭建 主机:192.168.161.179 从机:192.168.161.180 1. 安装主redis 自己本地环境,关 ...
- Chrome检查更新总失败?安装细则讲解
现在 Google Chrome 的稳定版都已经发布 68.0 版本了,我机上还是 54, 本想在线更新一下,结果点击菜单项中的“关于 Google Chrome”后,进入的界面提示“更新失败(错误: ...
- C# 对象间的 深拷贝 实现
以下的这个类实现了 2个含有部分字段名字相同 的对象的 赋值拷贝. public class ShallowCopyHelper { public static voi ...