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 ...
随机推荐
- P1220 关路灯(区间规划)
题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯. 为了给村 ...
- 使用 gRPCurl 调试.NET 5的gPRC服务
介绍 你用过 Curl 吗?这个工具允许你通过 http 来发送数据,现在有一个适用于gGRPC的工具,gRPCurl,在本文中,我将介绍如何下载安装这个工具,然后通过这个工具调试我们.NET 5上面 ...
- 入门训练 - 蓝桥杯(Python实现)
A+B问题: 题目: 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 输入A.B,输出A+B. 输入格式 输入的第一行包括两个整数,由空格分隔,分别表示A.B. 输出格式 输出一行, ...
- dblink查找对应的目标端session
v$session试图中process字段代表的是客户端所在机器的进程号 例如我使用toad连接数据库,查询到的process即toad的进程号 SELECT process FROM V$SESSI ...
- 08--Docker安装Mysql
1.在hub.docker.com中查找5.7版本 2.拉取mysql docker pull mysql:5.7 3.启动mysql镜像 docker run -p 3306:3306 --name ...
- Percona Toolkit工具使用
Percona Toolkit简称pt工具-PT-Tools,是Percona公司开发用于管理MySQL的工具,功能包括检查主从复制的数据一致性.检查重复索引.定位IO占用高的表文件.在线DDL等 下 ...
- k8s-jenkins持续发布tomcat项目
k8s-jenkins持续发布tomcat项目 一.需求 这个实验前期后后搞了很久(公司经常插一些别的事过来,然后自己比较懒,再加上自己知识不够扎实).二进制部署完k8s集群就开始做jenkins持续 ...
- CentOS 7.4通过rpm包离线安装 Mysql8.0并部署主从复制(附从库备份脚本)
一. 部署MySQL (两个节点都做) 下载 rpm包 wget https://goodrain-pkg.oss-cn-shanghai.aliyuncs.com/mysql8.rpm tar xv ...
- 转 Fiddler5 发送HTTP请求
Fiddler5 发送HTTP请求 文章转自:https://www.cnblogs.com/zhengna/p/10879573.html 1.Fiddler Composer发送HTTP请求 C ...
- spring 之7种重要设计模式
Spring中涉及的设计模式总结 1.简单工厂(非23种设计模式中的一种) 实现方式: BeanFactory.Spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获 ...