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. Shell脚本互斥设置

    参考:https://blog.csdn.net/hanjiezz/article/details/79571703 shell脚本为了防止同时执行相同的脚本需要设置互斥锁 简单方法,脚本执行开始生成 ...

  2. vscode springboot logback 日志输出到不同文件

    参照了:https://blog.csdn.net/appleyk/article/details/78717388# 在src\main\resources中新建一个logback-boot.xml ...

  3. charles Windows 安装

    本文参考:charles Windows 安装 在实际开发.测试中需要通过代理截取 app 的网络请求报文来快速定位问题.https 双向认证的 app 越来越多,fiddler在这方面并不好用.由于 ...

  4. phpstudy 8.0 安装redis并使用,解决phpstudy 8.0直接启用redis使用不了

    目前phpstudy 8.0直接安装redis启用,是用不了的 在phpstudy 8.0环境面板中找到redis安装后: 打开redis安装目录配置文件redis.windows.conf配置red ...

  5. Elasticsearch 全文搜索

    1,匹配查询(match) match查询主要的应用场景是进行全文搜索: // 1,初始化数据 DELETE /my_index PUT /my_index { "settings" ...

  6. MapReduce示例式理解

    从word count这个实例理解MapReduce. MapReduce大体上分为六个步骤:input, split, map, shuffle, reduce, output.细节描述如下: 1. ...

  7. Tomcat报错:No result type specified for result named 'success'

    今天学Struts, tomcat报出了异常信息 Exception starting filter [struts2] Unable to load configuration.还有 No resu ...

  8. Python基础 — 数据类型转换

    Python 数据类型转换 有时候,我们需要对数据内置的内心进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. 以下几个内置的函数可以执行数据类型之间的转换,这些函数返回一个新的对象,表示转 ...

  9. Ubuntu中更改默认的root用户密码,以及怎样修改用户密码

    新安装的Ubuntu系统中默认的root用户密码是多少?该怎么修改? 如题,相信许多刚接触Ubuntu系统的新手大多会遇到这个问题,那么我们该如何解决这个问题呢?Ubuntu在安装过程中并没有让我们设 ...

  10. Scratch编程:快乐的小马(三)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这是一款简单的小游戏,实现了一匹小马跑来跑去(小马有跑动 ...