Balanced Photo(USACO)
题目大意:
我们有一个数列,数列中有n个数,对于一个数ai,在它左边的比他大的数的个数为li,右边比他大的数的个数为ri,若li,ri中的较大者比较小者的两倍还大,那么他就是一个不平衡数,求不平衡数的数量。
————————————————我是分割线————————————————
好吧,典型逆序对。
因为我们要求左边比他大的数的个数,所以我们倒着排序,然后再离散。
然后树状数组解决问题。
那么右边怎么办?
很显然我们得到的离散数组的值就是比他大的数的个数+1(它本身),所以右边的答案为:离散数组值-左边比他大的数的个数-1;
然后轻松统计答案
下面贴代码
#include<cstdio>
#include<algorithm>
#define MN 100005
using namespace std;
int n,tot;
int b[MN],t[MN];
struct qaq{
int num,opt;
}a[MN];
int lowbit(int x){return (x&-x);}
int query(int x)
{
int ans=;
while(x)
{
ans+=t[x];
x-=lowbit(x);
}
return ans;
}
void update(int x){
while(x<=n)
{
t[x]++;
x+=lowbit(x);
}
}
bool cmp(qaq a,qaq b){return a.num>b.num;}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i].num),a[i].opt=i;
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)b[a[i].opt]=i;
for(int i=;i<=n;i++)
{
int l=query(b[i]),r=b[i]-l-;
if ((l*<r)||(r*<l))tot++;
update(b[i]);
}
printf("%d\n",tot);
// fclose(stdin);
// fclose(stdout);
}
Balanced Photo(USACO)的更多相关文章
- 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片
P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...
- 【USACO】 Balanced Photo
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...
- Balanced Teams (USACO Jan Bronze 2014)
既然是bronze,毫无压力的AC了. 就是个深搜,当然加个剪枝--最后一个组不用搜. 恩可以一个一个组分层次dfs,这样会跑得飞起~~也不容易错 #include <cstdio> in ...
- bzoj4759 [Usaco2017 Jan]Balanced Photo
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4759 [题解] 排序,从大到小插入,树状数组统计. # include <vector ...
- 【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 ...
- 【题解】[USACO17JAN]Balanced Photo G
题目链接:https://www.luogu.com.cn/problem/P3608 方法一 用树状数组求逆序对先后扫两遍,一次从前往后,一次从后往前,算出每头奶牛左右两边比她高的数量. 最后统计一 ...
- USACO比赛题泛刷
随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...
随机推荐
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- [CodeForces948D]Perfect Security(01字典树)
Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...
- 牛客暑假多校第二场J-farm
一.题意 White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. T ...
- tomcat7 配置 https安全访问
在apache-tomcat-7.0.33-windows-x64.zip配置https,结果在配置SSL时遇到一些问题 1.用JDK自带的keytool来生成私有密钥和自签发的证书,如下: keyt ...
- TouTiao开源项目 分析笔记10 实现通用普通文章片段页面
1.RxJava的Observable数据操作符总结 1.1.Map操作符 Map操作符对原始Observable发射的没一项数据应用一个你选择的函数, 然后返回一个发射这些结果的Observable ...
- 4 CSS的20/80个知识点
1.css的基本构成 样式选择器 id选择器 元素选择器 2.css的盒模型 border padding margin 3.Atom快捷键 4.程序 (1)初始程序 <!DOCTYPE htm ...
- P2985 [USACO10FEB]吃巧克力Chocolate Eating
P2985 [USACO10FEB]吃巧克力Chocolate Eating 题目描述 Bessie has received N (1 <= N <= 50,000) chocolate ...
- 【02】webstorm配置babel转换器+截图(by魔芋)
[02]webstorm配置babel转换器+截图(by魔芋) [02]魔芋的安装过程 01,配置babel. 02,用webstorm.注意webstorm的版本号. 03,使用 ...
- dubbo本地搭建实例
项目文件下载地址:http://download.csdn.net/detail/aqsunkai/9552711 概述 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服 ...
- 很全的 Python 面试题
很全的 Python 面试题 Python语言特性 1 Python的函数参数传递 看两个例子: Python 1 2 3 4 5 a = 1 def fun(a): ...