USACO 2017 January Contest, Gold Problem 1. Balanced Photo
来源:互联网 发布:python绝技 pdf 中文 编辑:程序博客网 时间:2024/05/19 06:35
利用树状数组,由于1e9太大,所以先进行排序,按数字大小进行排名。
#include<iostream>#include<algorithm>#include<cstring>#define MA 100005using namespace std;class node {public: int id; int w; int rank; int LN, RN;};int pre[MA];//树状数组int lowbit(int v){ return v&-v;}void join(int index, int val){ while (index < MA) { pre[index] += val; index += lowbit(index); }}int GetSum(int index){ int sum = 0; while (index) { sum += pre[index]; index -= lowbit(index); } return sum;}node all[MA];int main(){ freopen("bphoto.in", "r", stdin); freopen("bphoto.out", "w", stdout); int N; scanf("%d", &N); for (int t = 0; t < N; ++t) { all[t].id = t; scanf("%d", &all[t].w); } sort(all, all + N, [](const node a, const node b) {return a.w > b.w; }); all[0].rank = 1; for (int t = 1; t < N; ++t)//按数字大小进行编号 if (all[t].w == all[t - 1].w) all[t].rank = all[t - 1].rank; else all[t].rank = t+1; sort(all,all+N, [](const node a, const node b) {return a.id < b.id; }); for (int t = N - 1; t >= 0; --t)//先计算出右边比此位置数字大的元素个数 { all[t].RN = GetSum(all[t].rank-1); join(all[t].rank, 1); } memset(pre, 0, sizeof(pre)); int cnt = 0; for (int t = 0; t < N; ++t)//先计算出左边比此位置数字大的元素个数,并进行讨论 { all[t].LN = GetSum(all[t].rank - 1); join(all[t].rank, 1); if (min(all[t].RN, all[t].LN) * 2 < max(all[t].RN, all[t].LN))++cnt; } printf("%d\n", cnt);}
0 0
- USACO 2017 January Contest, Gold Problem 1. Balanced Photo
- USACO 2017 January Contest, Gold Problem 2. Hoof, Paper, Scissors
- USACO 2012 January Contest, Gold Division Solution
- USACO 2016 JANUARY CONTEST, BRONZE PROBLEM 1. PROMOTION COUNTING
- USACO 2016 US Open Contest, Gold Problem 3. 248
- 【USACO 2013 January Gold】奶牛排队
- 【USACO 2013 January Gold】座位 --线段树
- [USACO 2012 January Gold] Video Game Combos
- P2052【USACO 2013 January Gold】座位
- P2770【USACO 2014 January Gold】难度系数
- 【poj3264】Balanced Lineup 【USACO 2007 January Silver】
- USACO 2016 JANUARY CONTEST, BRONZE PROBLEM 3. MOWING THE FIELD(收割庄稼)
- USACO 2012 January Contest, Bronze Division
- USACO 2012 January Contest, Silver Division Solution
- usaco contest 2008.11 gold 安慰奶牛
- Balanced Photo
- JZOJ2935. 【USACO Open 2012 Gold Division】Balanced Cow Subsets
- USACO 2016 FEBRUARY CONTEST, BRONZE PROBLEM 1. MILK PAILS
- 【C++学习笔记】宽字符(LPCWTR/wstring)和(char*/string)的转换实现
- 杭电1029 之 Ignatius and the Princess IV
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 32-35: ordinal not in range(12
- openglesForC++矩形旋转
- 集合框架-Collections
- USACO 2017 January Contest, Gold Problem 1. Balanced Photo
- hololens
- Spring MVC常用的注解
- KM算法
- 单页应用跳转实现浅析-demo篇
- Appium 日常干货分享
- Vector,ArrayList,LinkedList,SynchronizedList
- splinter安装与入门
- Java事务处理全解析(四)—— 成功的案例(自己实现一个线程安全的