Codeforces 1005 E2 - Median on Segments (General Case Edition)
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(1, i) ,那么对于这个右端点,我们只要找到之前的 sum 中 < sum(1, i)的个数(左端点的个数),这个可以用树状数组维护
但是我们有一个O(n)的方法求,用了类似莫队的方法,记s[i]为之前的sum为i的个数,add为上一个小于sum(1, i-1)的个数,对于当前的sum,
如果它要加1,add += s[sum], sum++
如果它要减1,sum --, add -= s[sum]
这样得出的add就是当前的小于sum(1, i)的个数
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
int a[N], cnt[N*], n, m;
LL solve(int m) {
int s = n;
mem(cnt, );
cnt[s] = ;
LL add = , ans = ;
for (int i = ; i <= n; i++) {
if(a[i] >= m) add += cnt[s], s++;
else s--, add -= cnt[s];
cnt[s]++;
ans += add;
}
return ans;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
printf("%lld\n", solve(m) - solve(m+));
return ;
}
Codeforces 1005 E2 - Median on Segments (General Case Edition)的更多相关文章
- 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 - 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 #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- 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 ...
- 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 #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...
随机推荐
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...
- ACM札记
1. 逗号表达式 在“计蒜客“的ACM教程中,看到这样一段很好的代码: int n; while (scanf("%d", &n), n) { //do something ...
- mysql主从(主备)同步一键配置,配自动检测功能
主从一键shell配置 做个笔记. #!/bin/bash #Mysql sync #chenglee #master机器ip MasterIP="192.168.137.174" ...
- centos6.9 svn提交更新到网站根目录
一.首先创建网站根目录 ~] # mkdir -pv /export/home/cms/www_dyrs ~] # svn co svn://127.0.0.1/svn1 /export/home/c ...
- Io流的使用
file的使用 文件流的使用 FileInpunStream FileOutOputStream ----------字符流 FileReader Filewriter --------字节流 ...
- 1.多表查询 => 转化为一张联合大表 2.可视化工具 3.pymysql模块
多表数据 create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); crea ...
- UML(统一建模语言)是通用的可视化标准建模语言。由构造块、公共机制、构架三部分组成。
UML UML(统一建模语言)是通用的可视化标准建模语言.由构造块.公共机制.构架三部分组成. 1.构造块:包括基本的UML建模元素(类.接口.用例等).关系(关联关系.依赖关系.泛化关系.实现关系) ...
- nohup 让进程在后台可靠运行的几种方法
1. nohup nohup 无疑是我们首先想到的办法.顾名思义,nohup 的用途就是让提交的命令忽略 hangup 信号. nohup 的使用是十分方便的,只需在要处理的命令前加上 nohup 即 ...
- oracle单行函数 之 转换函数
to_char(字符串 \ 列, 格式字符串):将日期或者数字变成为字符串显示 注意点:时间字符串或时间类型列 与 格式字符串 必须是一一对应,若是多了少了相关字符会报错(除了使用systemd ...
- P4609 [FJOI2016]建筑师
思路 裸的第一类斯特林数,思路和CF960G相同 预处理组合数和第一类斯特林数回答即可 代码 #include <cstdio> #include <cstring> #inc ...