4759: [Usaco2017 Jan]Balanced Photo
来源:互联网 发布:淘宝开店攻略创业篇 编辑:程序博客网 时间:2024/06/06 02:20
4759: [Usaco2017 Jan]Balanced Photo
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 103 Solved: 83
[Submit][Status][Discuss]
Description
Farmer John is arranging his N cows in a line to take a photo (1≤N≤100,000). The height of the iit
h cow in sequence is hihi, and the heights of all cows are distinct.As with all photographs of his c
ows, FJ wants this one to come out looking as nice as possible. He decides that cow ii looks “unbala
nced” if Li and RRi differ by more than factor of 2, where LiLi and RiRi are the number of cows tall
er than i on her left and right, respectively. That is, ii is unbalanced if the larger of Li and Ri
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.
农夫约翰正在安排他的N头牛拍照片, 每头牛有一个身高,从1到N编号,排列成一行(h1,h2…hn),每头牛i左边
比他高的牛的数量记为Li,右边比他高的牛的数量记为Ri,如果存在i满足max(Ri,Li)>2*min(Li,Ri)则这个牛i是
不平衡的,现在FJ需要你告诉他有多少头牛不平衡。
Input
The first line of input contains N. The next NN lines contain h1…hN
each a nonnegative integer at most 1,000,000,000.
输入第一行为N(N<=1e5),接下来的一行有N个数,每个数表示第i头牛的身高,不超过1e9
Output
Please output a count of the number of cows that are unbalanced.
输出有多少头牛是不平衡的
Sample Input
7
34
6
23
0
5
99
2
Sample Output
3
//在这个样例中,身高为34,5,2的牛是不平衡的
HINT
Source
Gold
[Submit][Status][Discuss]
usaco划水。。。
按权值排序完以后就可以树状数组大力统计啦。。。
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;const int maxn = 1E5 + 10;typedef pair <int,int> pr;#define fr first#define sc second#define mp(a,b) (make_pair((a),(b)))int n,Ans,c[maxn];pr p[maxn];inline int getint(){ char ch = getchar(); int ret = 0; while (ch < '0' || '9' < ch) ch = getchar(); while ('0' <= ch && ch <= '9') ret = ret * 10 + ch - '0',ch = getchar(); return ret;}int main(){ #ifdef DMC freopen("DMC.txt","r",stdin); #endif n = getint(); for (int i = 1; i <= n; i++) p[i] = mp(getint(),i); sort(p + 1,p + n + 1); for (int i = n; i; i--) { int A = 0,B = 0; for (int j = n; j > 0; j -= j&-j) B += c[j]; for (int j = p[i].sc; j > 0; j -= j&-j) A += c[j]; B -= A; if (A > B) swap(A,B); if ((A << 1) < B) ++Ans; for (int j = p[i].sc; j <= n; j += j&-j) ++c[j]; } cout << Ans << endl; return 0;}
- 4759: [Usaco2017 Jan]Balanced Photo
- Balanced Photo
- 4756: [Usaco2017 Jan]Promotion Counting
- BZOJ4756: [Usaco2017 Jan]Promotion Counting
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- USACO17JAN Balanced Photo
- [DP] BZOJ 4758 [Usaco2017 Jan]Subsequence Reversal
- 4760: [Usaco2017 Jan]Hoof, Paper, Scissors
- bzoj 4756: [Usaco2017 Jan]Promotion Counting
- 【bzoj1636】[Usaco2007 Jan]Balanced Lineup
- bzoj1636: [Usaco2007 Jan]Balanced Lineup
- 1636: [Usaco2007 Jan]Balanced Lineup
- bzoj 4756: [Usaco2017 Jan]Promotion Counting (线段树合并)
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting 线段树合并
- bzoj4756[Usaco2017 Jan]Promotion Counting 树上主席树
- 4756: [Usaco2017 Jan]Promotion Counting 线段树合并
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting dfs序+主席树
- 简单工厂模式(三)
- MySQL数据库水平分表策略--一致性hash
- webservice-cxf-与spring整合
- UEditor在JavaWeb中的应用
- 读书笔记《Effective C++》条款33:避免遮掩继承而来的名称
- 4759: [Usaco2017 Jan]Balanced Photo
- 单例模式
- linux下dnw下载驱动后无法操作的问题日常记录
- 飞机大战的第一个页面
- numpy 数据类型与 Python 原生数据类型
- 剑指offer(java实现)(1-->11)
- 适配器模式
- 初学Android之飞机大战(一)——游戏菜单
- BZOJ 4031([HEOI2015]小Z的房间-矩阵树定理+辗转相除)