Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence such th…
E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence suc…
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的数有\(y\)个,只有\(x=y\)或\(y-x\)=1时,中位数才能取到\(m\),记\(m\)在原数组的位置为\(pos\). ​ 于是,我们先遍历\([pos,n]\),记录区间\([pos,i]\)中大于\(m\)的数和小于\(m\)的数个数差,用桶记录差值的个数. 然后我们反着遍历\([1,pos]…
题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比大于m的数少一.因此,维护比m小和比m大的数就行. m~n进行处理,比m大的cnt++,比m小的cnt--,用vis数组记录每个cnt值的个数. m~1再进行处理,比m大的cnt++,比m小的cnt--,ans+=vis[cnt]+vis[cnt+1],vis[cnt]可以理解为是右边有cnt个比m…
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80985281  原博客 对样例1: 5 42 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[…
E2 - Median on Segments (General Case Edition) 思路: 首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - solve(m+1) 那么怎么计算sovle(m)呢? 对于一个区间[l,r],如果它的中位数大于等于m,那么这个区间中 (大于等于m的数的个数) > (小于m的数的个数) 如果记a[i]大于等于m为+1,小于m 为 -1,即 sum(l, r)  > 0 我们枚举右端点 i ,并且同时计算sum(…
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, >m的数 记为-1, 求其前缀,  我们将问题转变成求以<= m 的数作为中位数的区间个数, 答案就变为ans(m) - ans(m - 1),我们可以用上面求得的前缀用bit就能求出答案. #include<bits/stdc++.h> #define LL long long #d…
You are given an integer sequence a1,a2,…,ana1,a2,…,an. Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of median of al,al+1,…,aral,al+1,…,ar is exactly the given number mm. The median of a sequence is the value of…
参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意:求一段数列中,取其中中位数为m的子序列个数有几个: 思路:首先我们可以先求出——序列中大于等于 m的数占多数的子序列——有多少个.然后,再求出序列中大于等于m+1的数占多数的子序列有多少个. 前面序列的个数减去后面的序列个数,就是答案. 显然这两个个数的求法是一样的.具体来说, 因为要计算区间的大…
Atcoder 题面传送门 & 洛谷题面传送门 u1s1 Atcoder 不少思维题是真的想不出来,尽管在 Atcoder 上难度并不高 二分答案(这我倒是想到了),检验最上面一层的数是否 \(\ge x\). 我们将最底下一层的数中 \(\ge x\) 的 \(a_i\) 设为 \(1\),\(<x\) 的设为 \(0\),那么原题可以转化为每次操作对于相邻三个数,如果 \(1\) 的个数 \(\ge 2\),就在该位上填一个 \(1\),否则在该位上填一个 \(0\),求最终顶上的数是…