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 ...
随机推荐
- Ext JS学习第十六天 事件机制event(一) DotNet进阶系列(持续更新) 第一节:.Net版基于WebSocket的聊天室样例 第十五节:深入理解async和await的作用及各种适用场景和用法 第十五节:深入理解async和await的作用及各种适用场景和用法 前端自动化准备和详细配置(NVM、NPM/CNPM、NodeJs、NRM、WebPack、Gulp/Grunt、G
code&monkey Ext JS学习第十六天 事件机制event(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...
- shell函数传递带空格的参数
shell中的参数以空格为分割符,经常会碰到需要传递带空格的参数,例如传递带空格的文件名. 方法很简单:给参数加双引号. 但是实际效果要看你的函数内容,一种可能的情况是: 其实你真的传递进去了带空格的 ...
- 迁移EXT4
http://fanli7.net/a/JAVAbiancheng/ANT/20101003/43604.html 級別: 中級 Roderick W. Smith ,顧問和作家 2008 年6 月0 ...
- Codeforces 558(C、D、E)总结
558C 题意:给你n个数,可对每一个数进行操作(乘2或者除以2).求最少的操作使得全部的数都相等. 思路 : dp[ t ] 表示全部的数转化到 t 所需的最少操作, vis[ t ] 表示有多少数 ...
- List中remove元素的理解
今天写了个简单的list中remove元素的方法,结果报错... List<String> ll = Arrays.asList("1","2",& ...
- 搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
6月12日,搜狐新闻APP最新版本在华为应用市场正式上线啦! 那么,这一版本的搜狐新闻APP有什么亮点呢? 先抛个图,来直接感受下—— 模糊图片,瞬间清晰! 效果杠杠的吧. 而藏在这项神操作背后的 ...
- 【PyCharm编辑器】之引用selenium包提示错误:Unresolved reference 'selenium' less... (Ctrl+F1)
一.现象还原: 当新建.py文件时,需要引用selenium中的方法时,报错,提示红波浪线: Unresolved reference 'selenium' less... (Ctrl+F1) Thi ...
- SpringBoot 定时任务升级篇(动态修改cron参数)
需求缘起:在发布了<Spring Boot定时任务升级篇>之后得到不少反馈,其中有一个反馈就是如何动态修改cron参数呢?那么我们一起看看具体怎么实现,先看下本节大纲: ()简单方式:修改 ...
- 关于一致/非一致代码段与TSS 关系的个人看法
[0]概念定义 0.1)一致代码段: 简单理解,就是操作系统拿出来被共享的代码段,可以被低特权级的用户直接调用访问的代码, 但是特权级高的程序不允许访问特权级低的数据. 通常这些共享代码,是" ...
- 局部描述符表LDT的作用+定义+初始化+跳转相关
[0]写在前面 0.1)本代码的作用: 旨在说明局部描述符表的作用,及其相关定义,初始化和跳转等内容: 0.2)文末的个人总结是干货,前面代码仅供参考的,且source code from orang ...