题目:戳这里

百度之星初赛原题:戳这里

题意:n个不同的数,求中位数为m的区间有多少个。

解题思路:

此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比大于m的数少一。因此,维护比m小和比m大的数就行。

m~n进行处理,比m大的cnt++,比m小的cnt--,用vis数组记录每个cnt值的个数。

m~1再进行处理,比m大的cnt++,比m小的cnt--,ans+=vis[cnt]+vis[cnt+1],vis[cnt]可以理解为是右边有cnt个比m大的数的情况(n为奇数,vis[cnt+1]是右边有cnt个比m大的数的情况(n为偶数。(当然真正的数值并不代表这个意思,因为出现比m小的数时cnt--了,在这里形容是为了便于理解。

这样操作的意思是,先预处理[pos[m],r]的所有情况,在遍历[l,pos[m]]的所有情况时,直接求出答案。

代码比文字好理解:

 1 #include <bits/stdc++.h>
2 typedef long long ll;
3 const int maxn = 1e6+10;
4 const int inf = 0x3f3f3f3f;
5 const ll mod = 998244353;
6 using namespace std;
7 int a[maxn];
8 map<int,int>vis;
9 int main(){
10 int n, m;
11 scanf("%d %d", &n, &m);
12 int pos = 0;
13 for(int i = 1; i <= n; ++i) {
14 scanf("%d",a+i);
15 if(a[i] == m) pos = i;
16 }
17 int cnt = 0;
18 for(int i = pos; i <= n; ++i) {
19 if(a[i] > m) ++cnt;
20 if(a[i] < m) --cnt;
21 vis[cnt]++;
22 }
23 cnt = 0;
24 ll ans = 0;
25 for(int i = pos; i >= 1; --i) {
26 if(a[i] > m) --cnt;
27 if(a[i] < m) ++cnt;
28 ans += vis[cnt]+vis[cnt+1];//此时m左边的大小情况为cnt,要找右边为cnt和cnt+1的情况
29 }
30 printf("%lld\n", ans);
31 return 0;
32 }

1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】的更多相关文章

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

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

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

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

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

  4. Codeforces #496 E1. Median on Segments (Permutations Edition)

    http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...

  5. 无序数组求第K大的数

    问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...

  6. [leetcode]4. Median of Two Sorted Arrays俩有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. 无序数组的中位数(set+deque)hdu5249

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. 无序数组求第k大/第k小的数

    根据http://www.cnblogs.com/zhjp11/archive/2010/02/26/1674227.html 博客中所总结的7种解法,我挑了其中的解法3和解法6进行了实现. 解法3: ...

随机推荐

  1. IDEA安装codota插件和使用,开发人员的知心伙伴

    打开IDEA 点击左上角的File之后,如下图 成功后如图所示

  2. mysqlG基于TID模式同步报错 (Last_IO_Errno: 1236)

    mysqlG基于TID模式同步报错Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading da ...

  3. Py-上下文管理方法,描述符的应用,错误与异常

    上下文管理方法: 可以在exit里面弄一些内存清理的功能 class Open: def __init__(self,name): self.name=name def __enter__(self) ...

  4. code-server Command ' ' not found

    由于通过一些特殊的方式登录linux用户后,全局变量不会自动加载,需要在 vscode 的 bash terminal手动读取 输入 source /etc/profile 或者vim ~/.bash ...

  5. 简话http请求

    一.http请求概念: 1.是指从客户端到服务器端的请求消息.包括:消息首行中,对资源的请求方法.资源的标识符及使用的协议. 包括请求(request)和响应(response) 2.过程: 域名解析 ...

  6. CKafka 架构原理

    消息队列 CKafka 技术原理 - 产品简介 - 文档中心 - 腾讯云 https://cloud.tencent.com/document/product/597/10067 消息队列 CKafk ...

  7. Jsp数字格式化

    日期格式(2008年5月5日22点00分23秒) <fmt:formatDate value="<%=new Date() %>" pattern="y ...

  8. 【Java】构造方法

    成员变量声明时初始化和构造方法中初始化的区别 声明时为成员变量赋值,那每次创建这个类的对象都是同一个值. 构造方法初始化,每次创建对象时可以为每一个对象赋不同的值(此时要通过有参构造). 无返回值类型 ...

  9. 【BFS】hdu 1973 Prime Path

    题目描述: http://poj.org/problem?id=3414 中文大意: 使用两个锅,盛取定量水. 两个锅的容量和目标水量由用户输入. 允许的操作有:灌满锅.倒光锅内的水.一个锅中的水倒入 ...

  10. 2019牛客暑期多校训练营(第八场)A-All-one Matrices(单调栈+思维)

    >传送门< 题意:给你一个01矩阵,求出所有不可扩大的全为1的矩阵的个数 思路:比赛的时候想到了用单调栈,但是也只是想到了,并不知道怎么用,其实和之前求二维01矩阵中全为1的矩阵最大面积非 ...