USACO2011 November Gold Above the Median
来源:互联网 发布:php网站在线人数统计 编辑:程序博客网 时间:2024/06/11 20:51
问题 C: Above the Median
时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 5
[提交][状态][讨论版]
题目描述
Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure their heights; cow i has height H_i (1 <= H_i <= 1,000,000,000) nanometers--FJ believes in precise measurements! He wants to take a picture of some contiguous subsequence of the cows to submit to a bovine photography contest at the county fair.
The fair has a very strange rule about all submitted photos: a photograph is only valid to submit if it depicts a group of cows whose median height is at least a certain threshold X (1 <= X <= 1,000,000,000).
For purposes of this problem, we define the median of an array A[0...K] to be A[ceiling(K/2)] after A is sorted, where ceiling(K/2) gives K/2 rounded up to the nearest integer (or K/2 itself, it K/2 is an integer to begin with). For example the median of {7, 3, 2, 6} is 6, and the median of {5, 4, 8} is 5.
Please help FJ count the number of different contiguous subsequences of his cows that he could potentially submit to the photography contest.
输入
* Line 1: Two space-separated integers: N and X.
* Lines 2..N+1: Line i+1 contains the single integer H_i.
* Lines 2..N+1: Line i+1 contains the single integer H_i.
输出
* Line 1: The number of subsequences of FJ's cows that have median at least X. Note this may not fit into a 32-bit integer.
样例输入
4 6
10
5
6
2
样例输出
7
提示
FJ's four cows have heights 10, 5, 6, 2. We want to know how many contiguous subsequences have median at least 6.There are 10 possible contiguous subsequences to consider. Of these, only 7
have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10,5, 6}, {10, 5, 6, 2}.
这个题目在落谷有连接:https://www.luogu.org/problemnew/show/3031
题目的意思就是:给出一串数字,问中位数大于等于X的连续子串有几个。(这里如果有偶数个数,定义为偏大的那一个而非中间取平均)
树状数组的方法可以看下:http://blog.csdn.net/kanosword/article/details/52607044
不知道是不是数据太少 O(nlogn) 和 O(n) 的时间一样
#include<stdio.h>#include<iostream> #include <algorithm>#include<string.h>#include<vector>#include<math.h>#include<queue>#include<deque>#include<set>#define ll long long#define INF 0x3f3f3f3fconst int mod = 1e9+7;using namespace std;int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1) return KGCD(a, b>>1); if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);} int LCM(int a,int b){ return a/KGCD(a,b)*b; } inline ll qpow(ll n,ll m){n%=mod;ll ans=1;while(m){if(m%2) ans=(ans*n)%mod;m/=2;n=(n*n)%mod;}return ans;}inline ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}inline ll inv2(ll b){return qpow(b,mod-2);}int dir[5][2]={0,1,0,-1,1,0,-1,0};using namespace std;int sum[1000005];//记录前边所有的大小于x的和 int num[1000005];//记录那种i有多少个 int main(){int n,m,x;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&x);if(x>=m)//有多少符合题目的 从1-N sum[i]=sum[i-1]+1;elsesum[i]=sum[i-1]-1;} long long ans=0; int pre=0;//记录i-1的区间和 num[0]=1; //已经存在0的时候sum[0]=0 所以num[0]=1 for(int i=1;i<=n;i++) {if(sum[i]>sum[i-1])//如果当前可以符合题意pre=pre+num[sum[i]]+1;//加上i-1符合题意的区间 和 和sum[i]一样高的区间数目 再加上 当前这个else//不符合题意 {pre=pre-num[sum[i-1]]+1; //如果不符合题意 pre减去上一个区间添加的(后边详细注解) }num[sum[i]]++;ans=ans+pre; } printf("%lld\n",ans);} //因为它每次的波动最大就是1 所以如果当前小了,说明sum[i]==sum[i-2]所以直接翻转回去就OK了 //玄学思路 膜拜大佬
阅读全文
0 0
- USACO2011 November Gold Above the Median
- Binary Indexed Tree树状数组初步 & USACO2011 November Gold Above the Median
- TOJ 3896 Above the Median -- 分治 树状数组
- 洛谷P3031 中石油2557 Above the Median (数组模拟or树状数组求逆序数)
- POJ3253[USACO 2006 November Gold]--Fence Repair
- [USACO 2006 November Gold] 玉米地Corn Fields
- november
- Verify the existence of the above class
- Deliver of the dfo gold
- 10.6上课——problem1切割木板(USACO 2006 November Gold)
- bzoj2442[USACO2011 Open]Mowing the Lawn修建草坪
- 【poj2387】Til the Cows Come Home 【USACO 2004 November】
- The Application Server Gold Rush @ JDJ
- clan, in the languagealso,wow gold
- clan, in the languagealso,wow gold
- The absence admission Cheap WildStar Gold
- uvalive 6800 - The Mountain of Gold? 判负环
- UVa10107 What is the Median?
- 浏览器放大缩小事件
- java读取文本文件做简单处理
- iis6用一段时间卡顿,如何清理iis日志?
- 深入理解Java中static关键字
- C#学习笔记---lambda表达式(1)
- USACO2011 November Gold Above the Median
- 面试题-请设计一个一百亿的计算器
- 浏览器事件流
- 前端大牛们都学过哪些?
- [日推荐]『超级收藏夹』微信里的收藏利器
- 数据结构实验之二叉树五:层序遍历
- es6->Symbol
- 谷歌有阿尔法狗,我们有阿尔法蛋丨钛空智慧星球推荐
- [V8]找出可能影响性能的代码(模式)