AtCoder 2581 Meaningful Mean(树状数组+离散化)
来源:互联网 发布:白山网络人才网 编辑:程序博客网 时间:2024/06/08 16:45
E - Meaningful Mean
Time limit : 2sec / Memory limit : 256MB
Score : 600 points
Problem Statement
You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.
a has N(N+1)⁄2 non-empty contiguous subsequences, {al,al+1,…,ar} (1≤l≤r≤N). Among them, how many have an arithmetic mean that is greater than or equal to K?
Constraints
- All input values are integers.
- 1≤N≤2×105
- 1≤K≤109
- 1≤ai≤109
Input
Input is given from Standard Input in the following format:
N Ka1a2:aN
Output
Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.
Sample Input 1
3 6757
Sample Output 1
5
All the non-empty contiguous subsequences of a are listed below:
- {a1} = {7}
- {a1,a2} = {7,5}
- {a1,a2,a3} = {7,5,7}
- {a2} = {5}
- {a2,a3} = {5,7}
- {a3} = {7}
Their means are 7, 6, 19⁄3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a1} and {a3} are indistinguishable by the values of their elements, but we count them individually.
Sample Input 2
1 21
Sample Output 2
0
Sample Input 3
7 2610203040302010
Sample Output 3
13
这道题给了一串整数,要求的是算数平均数大于等于K的区间的数目。也就是a1,a2,a3,...,an,求任意[l,r]的区间和s(其中l <= r),若s/(r-l+1) >= K,则给答案加1。为了便于观察,我们将下标改为从0开始,用下面这种形式变化,可以得到br >= bl这种形式的式子,那么问题就转化成了求一对l和r(其中0 <= l < r <= n),使得bl <= br的l和r的对数,也就是求顺序对的对数。
很自然地就能想到用树状数组来维护,由于b可能非常大,所以要进行离散化。离散化的方式在这里有两种方式,
一是将b的值进行排序,从小到大扫一遍,找前面的数里头有多少下标小于等于当前数(由于下标不会重复,所以与严格小于是一样的),然后加起来,不过要尤其注意排序的时候若b值相等,要将下标小的排在前面,并且得注意处理0下标。
二是将b原序保留在s里,再对b排序去重离散化,然后把s扫一遍,找直到当前下标有多少个数小于等于当前数。
显然第二种方法又要排序,又要去重,又要保留原数组,所以时空复杂度都稍逊于第一种方法,但是第一种方法较易出错。我自己先写的第一种总是WA,看了同学代码后写出了第二种,然后突然明白第一种应该要双关键字排序,又改了改才对了。这里也附上两种写法。
【代码1】
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=200005;struct node{ long long num; int id; bool operator<(const node &another)const { if(num==another.num)return id<another.id; return num<another.num; }};int n,k;long long a[MAXN];node b[MAXN];int lowbit(int x){ return (x&-x);}void modify(int x,int num){ if(x==0){ a[x]+=(long long)num; return; } while(x<=n){ a[x]+=(long long)num; x+=lowbit(x); }}long long sum(int x){ long long ans=a[0]; while(x>0){ ans+=a[x]; x-=lowbit(x); } return ans;}int main(){ scanf("%d %d",&n,&k); b[0].num=0;b[0].id=0; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); b[i].num=b[i-1].num+(long long)(x-k); b[i].id=i; } sort(b,b+1+n); long long cnt=0; memset(a,0,sizeof(a)); for(int i=0;i<=n;i++){ if(b[i].id>0)cnt+=sum(b[i].id-1); modify(b[i].id,1); } printf("%lld\n",cnt); return 0;}
【代码2】
#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespace std;const int MAXN=200005;int n,k;long long a[MAXN],s[MAXN],b[MAXN];map<long long,int> mp;int lowbit(int x){ return (x&-x);}void modify(int x,int num){ while(x<=n+1){ a[x]+=(long long)num; x+=lowbit(x); }}long long sum(int x){ long long ans=0; while(x>0){ ans+=a[x]; x-=lowbit(x); } return ans;}int main(){ scanf("%d %d",&n,&k); s[0]=0;b[0]=0; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); s[i]=s[i-1]+(long long)(x-k); b[i]=s[i]; } sort(b,b+1+n); int tot=0; for(int i=0;i<=n;i++) if(!mp.count(b[i]))mp[b[i]]=++tot; long long cnt=0; memset(a,0,sizeof(a)); for(int i=0;i<=n;i++){ cnt+=sum(mp[s[i]]); modify(mp[s[i]],1); } printf("%lld\n",cnt); return 0;}
- AtCoder 2581 Meaningful Mean(树状数组+离散化)
- ARC 075E Meaningful Mean 枚举+树状数组
- 树状数组--离散化
- hud 4358(树状数组+离散化)
- poj2299 (树状数组+离散化)
- 高桥低桥(树状数组离散化)
- Ultra-QuickSort(树状数组 + 离散化)
- POJ 2299(树状数组,离散化)
- HDU_5792_WorldIsExploding(树状数组&&离散化)
- 树状数组+离散化(hdu 5862)
- poj2299Ultra-QuickSort(树状数组+离散化)
- 计蒜客 排序(离散化+树状数组)
- hdu 5877(树状数组+离散化)
- Turing Tree(树状数组+离散化)
- Flowers--(树状数组,离散化)
- Flowers&&树状数组+离散化
- hdu4417(离散化+树状数组)
- POJ2299 树状数组+离散化
- 摄像头启动,开始监控
- oracle的安装
- P6 PPM EPPM 17.7 发布并下载
- 踏上公司作为码农的第一天--内存溢出 java.lang.OutOfMemoryError: PermGen space
- aardio学习记录-3.使用Vue+JSON实现aardio与HTML内容互动
- AtCoder 2581 Meaningful Mean(树状数组+离散化)
- springmvc的注解
- linux新系统干点啥?
- 前端框架Vue(15)——vue-cli 仿网易云音乐 Demo,环境搭建到开发 Vue 全家桶练手项目
- Servlet的学习
- memccpy()函数介绍
- Servlet简介及其生命周期详解
- 数据结构实验1-日期:9月8日
- 爬格子呀4-4