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 题意 ...
随机推荐
- 管道式编程(Pipeline Style programming)
受 F# 中的管道运算符和 C# 中的 LINQ 语法,管道式编程为 C# 提供了更加灵活性的功能性编程.通过使用 扩展函数 可以将多个功能连接起来构建成一个管道. 前言 在 C# 编程中,管道式编程 ...
- webpack package code into different bundle
Demo4操作手册 本Demo演示如何进行分块打包等较高级的使用 准备环境 初始化环境, cd到demo1目录之后, 执行如下命令: npm init -y npm install webpack w ...
- [bzoj4345][POI2016]Korale_堆_贪心_线段树_dfs
bzoj4345 POI2016 Korale 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4345 数据范围:略. 题解: 由于$k$的范围问 ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- LeetCode 242. 有效的字母异位词(Valid Anagram)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
- [转帖]linux操作系统测试工具
linux操作系统测试工具 http://cfdtesting.com/879156.html 作者: minions_222 来源: CFDTesting.com采编 发布于: ...
- Javascript 闭包何时回收?
定义 闭包是函数和声明该函数的词法环境的组合.闭包是指有权访问另一个函数作用域中的变量的函数,创建闭包的最常见的方式就是在一个函数内创建另一个函数,通过另一个函数访问这个函数的局部变量. 范例 fun ...
- Web基础和servlet基础
TomCat的目录结构 Bin:脚本目录(存放启动.关闭这些命令) Conf:存放配置文件的目录 Lib:存放jar包 Logs: 存放日志文件 Temp: 临时文件 Webapps: 项目发布目录 ...
- find_element_by_xpath()的几种方法
Xpath (XML Path Language),是W3C定义的用来在XML文档中选择节点的语言一:从根目录/开始有点像Linux的文件查看,/代表根目录,一级一级的查找,直接子节点,相当于css_ ...
- VMware虚拟机(Ubuntu)通过主机代理实现——浏览器+终端访问外网
环境说明:主机win10 + 虚拟机ubunut16.04 + 主机s-h-a-d-o-w-socks win10 主机相关操作配置1: 按下 Win + R 快捷键,输入 cmd ,然后在命令行中输 ...