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

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

对样例1:

  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的位置)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include <cctype>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cmath>
  7. #include<string>
  8. #include<cmath>
  9. #include<set>
  10. #include<vector>
  11. #include<stack>
  12. #include<queue>
  13. #include<map>
  14. using namespace std;
  15. #define ll long long
  16. #define mem(a,x) memset(a,x,sizeof(a))
  17. #define se second
  18. #define fi first
  19. const int INF= 0x3f3f3f3f;
  20. const int N=2e5+;
  21.  
  22. int n,m,pos,a[N];
  23. map<int,ll> mp;
  24.  
  25. int main()
  26. {
  27. cin>>n>>m;
  28. for(int i=;i<=n;i++){
  29. scanf("%d",&a[i]);
  30. if(a[i]==m) pos=i;
  31. }
  32. int num=,cnt=;
  33. for(int i=pos;i<=n;i++)
  34. {
  35. if(a[i]>m) cnt++;
  36. if(a[i]<m) cnt--;
  37. mp[cnt]++;
  38. }
  39. num=,cnt=;
  40. ll ans=;
  41. for(int i=pos;i>=;i--) // 2 4 5 3 1
  42. {
  43. if(a[i]<m) cnt++;
  44. if(a[i]>m) cnt--;
  45. ans+=mp[cnt]+mp[cnt+];
  46. }
  47. cout<<ans;
  48. }

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. MYSQL:基础——索引原理及慢查询优化

    MYSQL:基础——索引原理及慢查询优化 索引的数据结构 索引的数据结构是B+树.如下图所示,B+树的节点通常被表示为一组有序的数据项和子指针.图中第一个节点包含数据项3和5,包含三个指针,第一个指针 ...

  2. WPF TextBlock 文本换行的两种方式

    第一种: <TextBlock> This is line 1.<LineBreak/> This is line 2. </TextBlock> 第二种 < ...

  3. 关于mathtype6.9在office2010中出现The MathType can not be found的问题

    本文问题解决方法参考:https://blog.csdn.net/yiran103/article/details/41694843 自从重装了系统,安装mathtype总是提示The MathTyp ...

  4. eclipse的maven中需要把jar的包文件登入到自己的仓库里面的操作

    问题的描述 从别人那拿到了Java maven的工程,导入自己的eclipse中之后编译的时候出现包文件找不到,之后把工程进行maven的update project之后,pom.xml文件出现错误, ...

  5. HTTP权威指南-报文与状态码

    所有的报文都向下流动 报文流向 报文组成 HTTP方法 状态码 GET示例 HEAD示例 100~199 信息性状态码 200~299 成功状态码 300~399重定向状态码 400~499 客户端错 ...

  6. django使用pyecharts(2)----django加入echarts_前后台分离

    二.Django 中使用 pyecharts. 前后端分离 1.安装 djangorestframework linux pip3 install djangorestframework window ...

  7. 第二章 Python基础语法

    2.1 环境的安装 解释器:py2 / py3 (环境变量) 开发工具:pycharm 2.2 编码 编码基础 ascii ,英文.符号,8位为一个东西,2**8 unicode ,万国码,可以表示所 ...

  8. UNIX环境高级编程笔记 目录

    每一章的重点会使用加粗字体 第一章:UNIX基础知识:UNIX体系结构:文件和目录:输入和输出:程序和进程:出错处理:信号:时间值:系统调用和库函数 第三章:文件I/O:文件描述符:文件操作函数:文件 ...

  9. javascript:void(0);的含义以及使用场景

    一.含义: javascript:是伪协议,表示内容通过javascript执行. void(0)表示不作任何操作. 二.使用场景 1.href=”javascript:void(0);” 作用:为了 ...

  10. c++中配置多个环境的格式

    例:环境1=$(QTDIR)\bin 环境2=$(PATH) 环境3=E:\软件\办公\Vimba_2.1\VimbaCPP\Bin\Win64 具体配置为 PATH=$(QTDIR)\bin%3b$ ...