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 题意 ...
随机推荐
- Data - 深入浅出学统计 - 下篇
本文是已读书籍的内容摘要,少部分有轻微改动,但不影响原文表达. :以漫画形式来讲解最基本的统计概念和方法. ISBN: 9787121299636 https://book.douban.com/su ...
- AI - TensorFlow - 示例04:过拟合与欠拟合
过拟合与欠拟合(Overfitting and underfitting) 官网示例:https://www.tensorflow.org/tutorials/keras/overfit_and_un ...
- clickHouse可视化查询工具
clickHouse以卓越的查询性能著称,目前在大数据的存储和分析领域有广泛应用,目前TreeSoft已支持clickHouse的数据在线查询分析,可以与Mysql,oracle等数据库并存操作. 1 ...
- Ubuntu与Window双系统安装的注意事项
UBUNTU与WINDOW双系统安装的注意事项 Ubuntu与Window双系统安装的注意事项 由 匿名 (未验证) 提交于 2019-05-18 10:07:41 登录 发表评论 29 次浏览 ...
- 【计算机视觉】车牌识别开源框架EasyPR介绍
之前学习了一个GitHub开源的框架,GitHub地址为: https://github.com/liuruoze/EasyPR 希望通过此篇博客详细阐述如何一步步实现车牌的识别过程. 车牌识别分 ...
- MyBatis逆向工程生成配置 generator (生成pojo、mapper.xml、mapper.java)
MyBatis逆向工程生成 mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java.mapper.xml ...
- 字符串char vchar性能对比补充
Value CHAR(4) Storage Required VARCHAR(4) Storage Required '' ' ' 4 bytes '' 1 byte 'ab' 'ab ' 4 ...
- Python3遍历指定文件夹下所有文件及文件夹
采用os模块儿: import os def get_filelist(dir): for home, dirs, files in os.walk(dir): print("####### ...
- c++ (1) c++ 与c 的区别
可以说c++ 语言在c基础上扩展了许多 在学习玩c语言之后 学习c++ 会发现容易一些 但是c++也有优越于c 的地方 c++ 与c 语言都属于本地编译型语言 ,直接编译成本地编译码,运行特别快 ...
- NIO-FileChannel源码分析
目录 NIO-FileChannel源码分析 目录 前言 RandomAccessFile 接口 创建实例 获取文件通道 FileChannelImpl 创建 写文件 读文件 修改起始位置 获取文件长 ...