http://codeforces.com/contest/1005/problem/E1 题目

https://blog.csdn.net/haipai1998/article/details/80985281  原博客

对样例1:

5 4
2 4 5 3 1

m=4,所以下标pos=2; 从pos往右遇到比m大的就cnt++,遇到小的就cnt--: 刚开始cnt=0;  mp[cnt]++

于是从pos开始到 n:   mp[0]=1,   mp[1]=1,   mp[0]=2 ,   mp[-1]=1;

接下来从pos向左, 遇到比m大的就cnt-- ,遇到比小的就cnt++ , 刚开始cnt=0

因为n为偶数时m要排在n/2 ,n为奇数时m要排在n/2+1;

所以  ans += mp[cnt] + mp[cnt+1]     比如i=pos时候,此时cnt=0, ans+=mp[0]+mp[1] (偶数的情况加上奇数的情况)

i=pos-1,即cnt=1,a[i]=2的时候,ans+=mp[1]+mp[2] (mp[1]即加到5的位置)

 #include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=2e5+; int n,m,pos,a[N];
map<int,ll> mp; int main()
{
cin>>n>>m;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==m) pos=i;
}
int num=,cnt=;
for(int i=pos;i<=n;i++)
{
if(a[i]>m) cnt++;
if(a[i]<m) cnt--;
mp[cnt]++;
}
num=,cnt=;
ll ans=;
for(int i=pos;i>=;i--) // 2 4 5 3 1
{
if(a[i]<m) cnt++;
if(a[i]>m) cnt--;
ans+=mp[cnt]+mp[cnt+];
}
cout<<ans;
}

Codeforces #496 E1. Median on Segments (Permutations Edition)的更多相关文章

  1. 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 ...

  2. Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)

    题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...

  3. CF1005E1 Median on Segments (Permutations Edition) 思维

    Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...

  4. 1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】

    题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比 ...

  5. Codeforces 1005 E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 思路: 首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - s ...

  6. 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, ...

  7. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

  8. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

  9. 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 题意 ...

随机推荐

  1. Java基础教程:枚举类型

    Java基础教程:枚举类型 枚举类型 枚举是将一具有类似特性的值归纳在一起的方法.比如,我们可以将周一到周日设计为一个枚举类型.彩虹的七种颜色设计为一个枚举类型. 常量实现枚举 我们通过定义常量的方式 ...

  2. WXS----数据类型

  3. IDEA将Git本地仓库Push至远程仓库

    转自:https://blog.csdn.net/qq_15653601/article/details/79870996 本地本地仓库项目: 配置Git自动识别本机Git配置

  4. miniconda3 安装tensorflow

    使用miniconda3进行安装 conda create -n tensorflow conda install tensorflow 输入下面的代码进行测试 import tensorflow a ...

  5. TCP/IP学习笔记11--无线通信: 无线通信的种类, 点对点通信协议

    IEEE802委员会主要指定了以下几种分类: PAN: personal aera network;  LAN: local area network; WAN: wide aera network ...

  6. Rust零碎总结

    1.Rust里没有null的概念,但是实际上有很多地方是需要null的概念的,这个时候就可以用Option来代替,它是泛型T的一个包装类,就是C#里的int?或Java里的Optional: [但反序 ...

  7. http,ftp服务的安装

    首先,要配置云源(在我上一篇博客中有云源配置的详细过程) 一.http服务的安装 1.安装 http   yum install -y httpd 2.启动http服务  systemctl  sta ...

  8. 第三章 hash是个啥玩意

    3.10 hash 什么是哈希? hash,一般翻译做散列.杂凑,或音译为哈希,是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值.这种转换是一种压 ...

  9. Python——成员变量

    一.类变量 在类命名空间内定义的变量就属于类变量,python允许使用类来修改.读取类变量. 例: class a: b = '我是类变量' def c(self): print (a.b) # 通过 ...

  10. 【Qt】Qt5.12连接MySQl5.7(亲自测试成功)

    目录 00. 目录 01. Qt5.12连接MySQL5.7测试代码 02. Qt5.12连接MySQL5.7问题描述 03. 解决办法 04. 结果测试 05. 打赏 00. 目录 01. Qt5. ...