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 ...
随机推荐
- gitignore有时候为啥过滤不了文件或目录
一.问题介绍 使用Git过程中,有时候我们想过滤项目中的部分文件,在.gitignore中加入该文件名称或该文件所在目录的名称,比如我们的项目日志文件(.log文件) 但是有时候发现不管用.不好使. ...
- python3-----多进程、多线程、多协程
目前计算机程序一般会遇到两类I/O:硬盘I/O和网络I/O.我就针对网络I/O的场景分析下python3下进程.线程.协程效率的对比.进程采用multiprocessing.Pool进程池,线程是自己 ...
- Confluence 6 教程:空间高手
这个教程将会带你如何在 Confluence 中创建和自定义空间,同时也包括如何删除空间,如果需要的话.通过这个教程,你将成为使用空间的高手. 你需要具有创建空间(Create space)和创建个人 ...
- python基础之 序列 pickle&json
内容梗概: 1. 什么是序列化 2. pickle(重点) 3. shelve 4. json(重点) 5. configparser模块 1. 什么是序列化 在我们存储数据或者网络传输数据的时候. ...
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- 函数和函数模版在一个。cpp中的情况!(除了左移和右移,其他的不要用友元函数!!!)
// 友元函数和运算符重载的碰撞.cpp : 定义控制台应用程序的入口点. // #include <iostream> using namespace std; template < ...
- 配置ssh免密登录
安装ssh sudo apt-get install ssh 产生密钥:ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa (rsa为ssh的加密方式 ...
- ccf跳一跳
才考完,没题目,先传代码... #include<stdio.h> #include<string.h> int main() { int flag=0; int a[105] ...
- 创建型模式篇(工厂模式Factory Pattern)
一.工厂模式(Factory Pattern) 1.定义: 在软件系统,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.提供一种封 ...
- git reset --hard 恢复
git reset --hard ,再然后,悲剧上演~ 恢复方法: 使用 git reflog 来找到最近提交的信息,这里贴出部分信息: F:\voidy>git reflog WARNING: ...