CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)
You are given an integer sequence a1,a2,…,ana1,a2,…,an.
Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of median of al,al+1,…,aral,al+1,…,ar is exactly the given number mm.
The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.
For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.
Write a program to find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of median of al,al+1,…,aral,al+1,…,ar is exactly the given number mm.
Input
The first line contains integers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105) — the length of the given sequence and the required value of the median.
The second line contains an integer sequence a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105).
Output
Print the required number.
Examples
5 4
1 4 5 60 4
8
3 1
1 1 1
6
15 2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
97
Note
In the first example, the suitable pairs of indices are: (1,3)(1,3), (1,4)(1,4), (1,5)(1,5), (2,2)(2,2), (2,3)(2,3), (2,5)(2,5), (4,5)(4,5) and (5,5)(5,5).
题意:给定N,M,已经N个数。问有多少哥区间,排序后其中位数是M。
思路:这个序列中如果只有一个M,那么可以理由前缀和的思路求解。我们把大于M的数看成1,小于M的数看成-1,那么问题就成了有多少个区间的区间和为0或者1,直接用map搞前缀和即可。现在有多个M,那么再这么搞可能会重复。
我们把此题用函数的思想来搞,同样的,把大于等于M的数看成1,小于M的数看成-1,令F(x)表示M=x的时候,有多少个区间和大于0。
那么结果就是F(M)-F(M+1)。那么现在问题就算求解函数F。
对于函数F(x),把大于大于x的数转化为1,否则为-1,那么现在数列是一系列的1和-1串,记录前缀和,然后可以用树状数组搞定即可,复杂度为O(N*lgN)。
但是由于只有1和-1,我们耶可以利用其特殊性,保留有效信息,把复杂度做到O(N);now表示前缀和,delta表示前面有多个位置可以满足区间和大于0,sum[x]表示前缀和为x的个数,那么新加入一个1时,delta显然会增加sum[now]个,然后now++。 加入一个-1时,delta会减少sum[now-1]个,now--。
(此题转化为函数的思想,然后做减法,妙的。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int N,M,a[maxn],sum[maxn]; ll ans;
ll solve(int num)
{
memset(sum,,sizeof(sum));
int now=N; ll res=,delta=; sum[now]=;
for(int i=;i<=N;i++){
if(a[i]>=num) delta+=sum[now],now++;
else now--,delta-=sum[now];
res+=delta;
sum[now]++;
}
return res;
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++) scanf("%d",&a[i]);
printf("%I64d\n",solve(M)-solve(M+));
return ;
}
CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)的更多相关文章
- Codeforces 1005 E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 思路: 首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - s ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)
参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...
- Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)
E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...
- CF1005E1 Median on Segments (Permutations Edition) 思维
Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
随机推荐
- Sqlserver 实际开发中表变量的用法
在实际的开发中,我们可能遇到的问题是,在一个存储过程里面,我们可能要返回多段sql的结果集,但是最终怎么把多个结果集合成一块呢,那么这个时候临时表变量就来了 declare @tmp table ...
- 结构体定义:struct与typedef struct
https://blog.csdn.net/haiou0/article/details/6877718?tdsourcetag=s_pcqq_aiomsg https://blog.csdn.net ...
- Rate Monotonic Scheduling algorithm
这篇文章写得不错 http://barrgroup.com/embedded-systems/How-To/RMA-Rate-Monotonic-Algorithm 另外rtems的官方文档也有类似说 ...
- Hadoop起源
本文来自Doug Cutting为<Hadoop权威指南>所作之序,感觉读一下还是挺有收获的. Hadoop 起源于Nutch项目.我们几个人有一段时间一直在尝试构建一个开源的Web搜索引 ...
- Perl语言学习笔记 15 智能匹配与give-when结构
1.智能匹配操作符 替代绑定操作符: 在哈希中查找某一个键: 比較两个数组是否全然同样: 查找列表中是否存在某个元素: 智能匹配操作符与顺序无关.~~ 左右元素能够互换 2.智能操作符优先级 3.gi ...
- JQuery的一些思想,自己的一些见解!!!!
自己总结了一下JQuery底层的一些思想,纯属于个人见解.. 为了方便描述,现在客户假如给了我们一个需求: 页面上有两个按钮,一张图片,当我点击hidden按钮时隐藏图片,当我点击show按钮时显示图 ...
- vue-strap 修改Modal组件
在用到vue-strap的Modal组件时,会有两个默认按钮,查看官方文档配置如下: 可以看到,ok-text和cancel-text都有一个默认值,在使用时即使不给这两个选项赋值,也会显示两个默认文 ...
- PowerBuilder -- 保存图片
String ls_path, ls_file_name, ls_filter, ls_errInt li_ret, li_loop, li_i, li_file, li_bytesLong ll_f ...
- 目标检测之行人检测(Pedestrian Detection)---行人检测之简介0
一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...
- Android TextView setText卡顿问题
TextView 是经常使用控件之中的一个,最经常使用的方法是setText() . 可是 我们在显示大量的文本的时候,使用setText还是会有一些性能的问题. 这篇文章 关于TextView的s ...