Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)
3 seconds
256 megabytes
standard input
standard output
You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.
Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.
The median of a sequence is the value of the 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 the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.
The first line contains integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤n1≤m≤n) — the length of the given sequence and the required value of the median.
The second line contains a permutation p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.
Print the required number.
5 4
2 4 5 3 1
4
5 5
1 2 3 4 5
1
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
48
In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).
题意:给出n个数,中位数m,求在这n个数中的任意区间内中位数是n的个数,区间个数是偶数的时候去左边的为 中位数
解题思路:刚开始我以为这是主席树的模板题,第k大,后来听别人说不用这么复杂,因为是n个数互不重复1-n,因为要求的区间里面肯定包含了m,所以我们先求出m的位置,然后我们仔细想
可以得知在这个区间里面要使中位数是m的话,奇数区间大于m的个数与小于m的个数是一样的,偶数区间是大于m的个数比小于m的个数多1,所以我们用map记录比m大和小的个数,我们先从
m的位置从右边遍历求出区间大于小于m的情况,用map 存大于m和小于m的差值,这样比较方便,比如mp[0]=1,说明右边大于m和小于m的区间个数相等的区间有1个,比如mp[-1]=2,说明右边
大于m比小于m少一个的区间个数有2个,以此类推,然后我们再此遍历左边,如果左边大于m的个数是1的话,奇数区间那么我就要右边小于m的个数为1,也就是mp[-1],偶数区间就要右边大于m
小于m个数相等,也就是mp[0],从而推出式子 cnt记录大于小于m的个数 sum=sum+mp[-cnt]+mp[1-cnt];
#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
int main()
{
map<ll,ll> mp;
ll m,n,a[];
cin>>n>>m;
int pos;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]==m)
pos=i;
}
int cnt=;
for(int i=pos;i<n;i++)
{
if(a[i]>m) cnt++;
if(a[i]<m) cnt--;
mp[cnt]++;
}
ll sum=;
cnt=;
for(int i=pos;i>=;i--)
{
if(a[i]>m) cnt++;
if(a[i]<m) cnt--;
sum=sum+mp[-cnt]+mp[-cnt];
}
cout<<sum;
}
Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)的更多相关文章
- Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...
- 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 #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- Codeforces Round #496 (Div. 3) ABCDE1
//B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...
- 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 #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #550 (Div. 3) E. Median String (模拟)
Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
C. Median Smoothing A schoolboy named Vasya loves reading books on programming and mathematics. He ...
随机推荐
- 部署--云服务器(RubyChina上的转帖); 附加用cap部署sidekiq
https://ruby-china.org/topics/36899 附加https://ruby-china.org/topics/36899 Capistrano + Rails5.2部署 使用 ...
- 微信小程序select不能使用,如何实现同样的效果
如果想实现同样的效果,只能使用小程序组件picker,其中,可以有一列,或者多列 点击链接查看详情: https://mp.weixin.qq.com/debug/wxadoc/dev/compone ...
- 【洛谷p1601】A+B Problem(高精)
高精度加法的思路还是很简单容易理解的 A+B Problem(高精)[传送门] 洛谷算法标签: 附上代码(最近懒得一批) #include<iostream> #include<cs ...
- iptables -F 与 -X 区别
test: 1.iptables 初始化 2.iptables -X (第一次) 错误原因是自定义链表(test)不为空 3.iptables -F 4.iptables -X ok,实验结束 实验报 ...
- 函数模版和主函数分别在.h .cpp中(要包含.cpp)
Complex.h #pragma once #include<iostream> using namespace std;//这句还必须加,要不然致错,不懂为啥呢 template &l ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- 转-如何使用iTunes制作iPhone铃声
新版iTunes(iTunes11)推出以后,界面上发生了一些改变,给人带来一种面貌一新的感觉,但也给许多朋友带来一些操作上的不太适应.下面就大家比较关心的iPhone的铃声制作方法,我在iTunes ...
- 动态BT跳转
METHOD eh_onzobject_id_link. ***定义 DATA : lv_crmt_object_guid TYPE crmt_object_guid, lv_index TYPE i ...
- Beta阶段——第4篇 Scrum 冲刺博客
Beta阶段--第4篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 昨日完成获取提醒语句的接口函 ...
- Cassandra标准列和超级列
列(column)是Cassandra数据模型中的最基本的数据结构单元.列是一个由列名(key).值(value).时间戳(timestamp)构成的三元组.在关系型数据库中,你需要先定义列的名称和和 ...