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

题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数).
题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的数有\(y\)个,只有\(x=y\)或\(y-x\)=1时,中位数才能取到\(m\),记\(m\)在原数组的位置为\(pos\).
于是,我们先遍历\([pos,n]\),记录区间\([pos,i]\)中大于\(m\)的数和小于\(m\)的数个数差,用桶记录差值的个数.
然后我们反着遍历\([1,pos]\),在段区间中,比\(m\)小的数可以和右边比\(m\)大的数抵消,比\(m\)大的数可以和右边比\(m\)小的数抵消,所以我们记录这些个数,然后每次更新一下答案即可(要考虑元素个数为偶数的情况且这题爆\(long\ long\)).
其实可能有点难理解,我个人认为可以这么想,假如我们不看左边的部分,那么对于右边的部分,只有当差值为\(0\)或\(1\)的情况才满足条件,而差值是\(0\)和\(1\)的所有情况当我第一次遍历左边的时候(\(m\)本身)就全部加到答案中了,然后再不断向左遍历,和右边相抵消.(比如说,我左边有\(3\)个连续比\(m\)小的数,那么此时\(cnt=3\),而对于右边而言,假如右边的差值为\(3\),也就是说相对比\(m\)大的数有\(3\)个,而此时我左边有\(3\)个比\(m\)小的数,那么他们就抵消了,这种情况自然也就成立,显然,当右边为\(4\)的时候,左边为\(3\)也是成立的).
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; int n,m;
ll a[N];
int pos,cnt;
map<int,ll> mp;
int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i){
cin>>a[i];
if(a[i]==m) pos=i;
}
for(int i=pos;i<=n;++i){
if(a[i]>m) cnt++;
else if(a[i]<m) cnt--;
mp[cnt]++;
}
cnt=0;
ll res=0;
for(int i=pos;i>=1;--i){
if(a[i]<m) cnt++;
else if(a[i]>m) cnt--;
res+=mp[cnt]+mp[cnt+1];
}
printf("%lld\n",res); return 0;
}
Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)的更多相关文章
- 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 ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- 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 题意 ...
- Codeforces #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...
- 1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】
题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比 ...
- Codeforces Round #496 (Div. 3) ABCDE1
//B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...
- CF1005E1 Median on Segments (Permutations Edition) 思维
Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...
- Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...
随机推荐
- Head First 设计模式 —— 14. 复合 (Compound) 模式
复合模式 在一个解决方案中结合两个或多个模式,以解决一般或重复发生的问题. P500 思考题 public interface Quackable { public void quack(); } p ...
- Nginx 路由转发和反向代理 location 配置
Nginx 配置的三种方式 第一种直接替换 location 匹配部分 第二种 proxy_pass 的目标地址,默认不带 /,表示只代理域名,url 和参数部分不会变(把请求的 path 拼接到 p ...
- Linux应急响应--入侵排查
1.入侵者可能会删除机器的日志信息,可以查看日志信息是否还存在或者是否被清空,相关命令示例: ll -h /var/log/* 系统日志一般都存在/var/log下常用的系统日志如下:核心启动日志: ...
- 【ORACLE】11g rac+dg
首先感谢群友分享的文档,在这里先感谢哆啦B梦,非常感谢 该文档主要指导如何利用现有的RAC环境搭建一套RAC与单实例的DG的环境 ============================主机配置信息 ...
- 腾讯云COS对象存储占据数据容灾C位
说到公有云容灾,大家首先想到的是云上数据备份. 然而,随着企业核心业务逐渐从线下迁移到云上,客户提出了更高的要求.如何确保云上业务的高可用.数据的高可靠,这对云厂商提出了新的挑战. 腾讯云作为全球领先 ...
- [Usaco2010 Hol]cowpol 奶牛政坛
题目描述: 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可 ...
- Django中多表关联的展示问题:
增加一个知识点,当表中有多对多关联时,在前端展示的时候是一个列表,所以在展示的时候需要这样做: 表结构: class ProjectEnv(models.Model): project = model ...
- top命令详解-性能分析
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析. top命令说明 [www.linuxidc.com@linuxidc-t-tomcat-1 ...
- Shell从入门到精通
熟悉基本shell操作不仅是运维的基本功,对于开发来说也是多多益善,我在学习的过程中,总结了十个练手的小demo,并附上涉及的知识点,仅供娱乐. 1. 多线程ping监控,检查同一网段的IP是否连通 ...
- C++ Primer Plus读书笔记(七)函数
1.声明 void fun1(); //该声明在C中的意思是不指定参数,但是在C++中是指定参数为void void func2(...); //C++中不指定参数 2.函数参数为数组区间 STL有数 ...