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. log4j 2 入门实例(2)

    本文介绍将日志输出到文件的例子. log4j 2输出到文件 log4j2.xml文件 这个文件里,定义了三个类型的Appender:Console.File和RollingFile. Console类 ...

  2. NodeJs如何全局统一处理异常,实现RestFull风格

    当在controller中处理客户端发来的数据时,我们会去校验数据,当数据错误时,我们会给客户端返回一个信息,如: export function add (req, res, next) { con ...

  3. BZOJ1833 数位DP

    数位DP随便搞搞. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...

  4. 51nod 1022 石子归并 V2 —— DP四边形不等式优化

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1022 1022 石子归并 V2  基准时间限制:1 秒 空间限 ...

  5. Smarty模板重点汇总

    Smarty模板重点回顾:1.功能:前后端分离:2.实现方法:通过使用Smarty的核心类来实现,利用display方法来读取模板文 件,用正则进行替换,替换完保存到临时文件,再将临时文件加载到当前页 ...

  6. display:inline-bock的注意

    前端当一组元素设置为display:inline-block;时,每个元素之间的回车会被作为一个空格.

  7. 【POJ 3580】SuperMemo Splay

    题意 给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$ ...

  8. 剑指OFFER18 判断一个二叉树的子树

    public class a18_IsSubTree { public static boolean hasSubTree(TreeNode treeRoot1, TreeNode treeRoot2 ...

  9. javascript基础知识整理(不定时更新)

    1.js中真与假的定义: 真:true,非零数字,非空字符串,非空对象 假:false,数字零,空字符串,空对象(null),undefined 2.使用for循环对json进行循环操作 for(va ...

  10. PS 滤镜— — sparkle 效果

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...