Codeforces #496 E1. Median on Segments (Permutations Edition)
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)的更多相关文章
- 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) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...
- CF1005E1 Median on Segments (Permutations Edition) 思维
Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...
- 1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】
题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比 ...
- Codeforces 1005 E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 思路: 首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - s ...
- 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, ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- 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 题意 ...
随机推荐
- Linux配置Docker镜像加速器
Docker默认镜像为官方镜像,可以配置成国内加速器提高速度 登录阿里云控制台,搜索容器镜像服务获取到镜像加速服务地址 新建配置文件 /etc/docker/daemon.json 输入以下内容 { ...
- 03.linux入门命令
1.linux命令的格式 命令 [选项] [参数] eg: ls ls -l ls -l /home 注: a.选项与参数不一定存在 b.选项用 "-" 来指明 c.命令,选项,参 ...
- C++ 优先队列priority_queue用法【转载】
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...
- solr查询返回有中括号【可用】
看图 解决方法: 两个core名称一样就对了 有些版本的solr就是schema.xml文件 这个方法好像不行,再找找看,先记录一下 2019-06-29 改完上面后要重启加载一下core 先看一下
- 漏洞复现之JBoss 4.x JBossMQ JMS 反序列化漏洞(CVE-2017-7504)
前言: 序列化就是把对象转换成字节流,便于保存在内存.文件.数据库中:反序列化即逆过程,由字节流还原成对象. Java中的ObjectOutputStream类的writeObject()方法可以实现 ...
- 虚拟机中Linux环境下使用Squid部署代理缓存服务(及透明传输)
小知识: 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低原服务器的负载. 不仅如此,还为读者们添加了对指定IP地址.网页关键词.网址与文件后缀的ACL访问限制功能的实 ...
- qmake持续学习
1 qmake技巧: https://blog.csdn.net/chenjianqi0502/article/details/79092433
- Recurrent neural network (RNN) - Pytorch版
import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # ...
- redis 主从 哨兵
数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...
- laravel框架视图中常用的逻辑结构forlese,foreach,ifelse等
if 和else @if($name === 1) 这个数字是1 @else 这个数字非1 @endif switch @switch($name) @case(1) 变量name == 1 @bre ...