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
256 megabytes
input
standard input
output
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) (1lrn1≤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) (1lrn1≤l≤r≤n) such that the value of the median of pl,pl+1,,prpl,pl+1,…,pr is exactly the given number mm.

Input

The first line contains integers nn and mm (1n21051≤n≤2⋅105, 1mn1≤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 (1pin1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.

Output

Print the required number.

Examples
input
Copy
5 4
2 4 5 3 1
output
Copy
4
input
Copy
5 5
1 2 3 4 5
output
Copy
1
input
Copy
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
output
Copy
48
Note 

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[200005];cin>>n>>m;int pos;for(int i=0;i<n;i++){cin>>a[i];if(a[i]==m)pos=i;}int cnt=0;for(int i=pos;i<n;i++){if(a[i]>m) cnt++;if(a[i]<m) cnt--;mp[cnt]++;}ll sum=0;cnt=0;for(int i=pos;i>=0;i--){if(a[i]>m) cnt++;if(a[i]<m) cnt--;sum=sum+mp[-cnt]+mp[1-cnt];}cout<<sum;
}

 

转载于:https://www.cnblogs.com/Lis-/p/9299800.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/413746.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数组转换为字符串方法

1. toString() 方法 和 toLocaleString() 方法 var arr [ "a", "b", "c"]; alert(arr.toString()); // a,b,c alert(arr.toLocaleString()); // a,b,c 返回数组的字符串表示&#xff0c;中间以逗号隔开 2. join() 方法 var a…

[vue-cli]vue-cli生成的项目可以使用es6、es7的语法吗?为什么?

[vue-cli]vue-cli生成的项目可以使用es6、es7的语法吗&#xff1f;为什么&#xff1f; vue-cli 配置了babel,可以将es6,es7....etc在webpack打包的时候转换成es5的代码&#xff0c;所以上线的时候没有问题。但是脚手架只是配置了一些默认常见的用法&#xff0c; 可以根据babel…

做小程序的流程总结(基本篇)

一、首先当我们借助小程序实现我们的网站搭建时&#xff0c;就需要使用小程序自带的一些功能&#xff1b;且需要根据该小程序获取到的一些参数存储到对应的数据库中。 openID&#xff1a;每个微信用户使用该小程序时都会产生一个openID&#xff0c;且该openID是唯一标识&#x…

js对象数组 按对象的某一属性进行去重

var array [{ id: 1, name: "张三"},{ id: 2, name: "李四"},{ id: 3, name: "张龙"},{ id: 4, name: "赵虎"},{ id: 5, name: "王朝"},{ id: 1, name: "刘金刚"},{ id: 6, name: "马汉"}, ]var obj …

[vue-cli]vue-cli怎么解决跨域的问题?

[vue-cli]vue-cli怎么解决跨域的问题&#xff1f; 在根目录下新建&#xff1a;vue.config.js注意名不能错误&#xff0c;然后里面配置 module.exports { devServer: { proxy: { //配置跨域 /api: { target: 跨域url, ws: true, changOrigin: true // pathRewrite: { // ^/api…

java - springmvc整合cxf发布webservice

1.jar包已上传百度云盘&#xff0c;在jar包目录下 2.web.xml配置 <web-app xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns"http://java.sun.com/xml/ns/javaee" xsi:schemaLocation" http://java.sun.com/xml/ns/javaee http://jav…

CSS3 选择前几个元素 选择后几个元素等问题

//例如有如下代码块 <div><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p> </div>1.选择第n个p div:nth-child(n) p:nth-of-type(n)2.选择倒数第n…

[vue-cli] vue-cli中你经常的加载器有哪些?

[vue-cli] vue-cli中你经常的加载器有哪些&#xff1f; style,css,vue,postcss,url等个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

Python中将array类型不按科学计数法存在文件中的方法

直接上代码&#xff1a; from numpy import *import numpy as npDrug_array zeros((708,708),dtype int)f open(D:\mat_drug_drug.txt)lines f.readlines()Drug_row 0for line in lines: list line.strip(\n).split( ) Drug_array[Drug_row:] list[:] Drug_row…

[vue-cli] 你知道什么是脚手架吗?

[vue-cli] 你知道什么是脚手架吗&#xff1f; vue项目一般是使用webpack进行打包构建的&#xff0c;然而如果每一个项目都需要我们去配置loader和plugin的话&#xff0c;是很重复的劳动&#xff0c;并且vue项目需要使用到的最基本的webpack loader和webpack plugin是相同的。因…

Vue报错:Elements in iteration expect to have ‘v-bind:key‘ directives的解决办法

1.我们在使用v-for的时候&#xff0c;在v-for 后添加 v-bind:key"ite Vue 2.2.0的版本里&#xff0c;当在组件中使用v-for时&#xff0c;key是必须的。 1.我们在使用v-for的时候&#xff0c;在v-for 后添加 v-bind:key"item" <div v-for"todo in to…

vue报错:error Strings must use singlequote quotes 字符串必须使用单引号

例出现下面报错 这个问题说明必须使用单引号&#xff0c;在vue的项目开发中&#xff0c;如果我们在通过vue-cli脚手架构建项目的时候使用了Eslint严格模式&#xff0c;那么对于字符串类型的数据String必须要使用单引号&#xff0c;不能使用双引号&#xff0c;否则会报异常。所以…

Java数组概述和定义

1、数组概述和定义格式说明    为什么要有数组(容器)&#xff1a;       为了存储同种数据类型的多个值  数组概念&#xff1a;      数组是存储同一种数据类型多个元素的集合。也可以看成是一个容器。      数组既可以存储基本数据类型&#xff0c;也可…

工作327:uni-数据格式处理

allPrpos(obj) { // 用来保存所有的属性名称和值let list[]var props "";// 开始遍历for(var p in obj){ // 方法if(typeof(obj[p])"function"){ obj[p]();}else{ // p 为属性名称&#xff0c;obj[p]为对应属性的值if(p.indexOf("apic") ! -1){…

vue eslint报错Expected linebreaks to be ‘LF‘ but found ‘CRLF‘

原因&#xff1a;在使用不同的编辑器和操作系统时&#xff0c;我们使用的换行操作不一样&#xff0c;造成了这种报错 解决方法&#xff1a;在.eslintrc.js文件里面&#xff0c;在 rule&#xff1a;下面加上”linebreak-style“:[0&#xff0c;“error”&#xff0c;“window”…

mysql数据库的设计

数据库的设计有一个严谨的流程&#xff0c;根据流程制作一个完整的数据库&#xff0c;可以省去很多的时间&#xff0c;也可以最大程度上与客户的想法契合。 需求分析阶段&#xff1a;分析客户的业务和数据处理需求 概要设计阶段&#xff1a;设计数据库的E-R模型图&#xff0c;确…

[vue-cli] 说下你了解的vue-cli原理?你可以自己实现个类vue-cli吗?

[vue-cli] 说下你了解的vue-cli原理&#xff1f;你可以自己实现个类vue-cli吗&#xff1f; 原理就是通过node环境发起git请求&#xff0c;把预先设置好的模板下载下来。 给时间的话&#xff0c;应该可以实现&#xff0c;需要用到一些npm包个人简介 我是歌谣&#xff0c;欢迎和…

配置svn 报错E200002解决办法

新买个mac配置svn&#xff0c;在修改配置文件svnserve.conf保存的时候 报了一个E200002错误 就很懵逼 去网上找了一下资料才了解原因&#xff1a; svnserve.conf 文件配置需要顶格写 不能换行或者空格&#xff01; 改完就对了&#xff01;

NTP服务器时间同步

一、简要说明 二、安装步骤 三、配置文件 四、常用命令 五、注意事项 六、运行截图 七、参考资料一、简要说明 搭建Kubernetes环境&#xff0c;需要几台、几十台机器配合运作&#xff0c;许多集群服务比如Etcd等都依赖系统的时间&#xff0c;如果机器的系统时间不一致…

[Css] 使用css如何拉伸字体?

[Css] 使用css如何拉伸字体&#xff1f; letter-spacing&#xff0c;transform:scale个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题