BZOJ 1303: [CQOI2009]中位数图( )
2015-07-21 21:11
393 查看
这种题做法应该很多吧...说说我的做法
设b出现位置为pos, 从pos开始向右扫一遍顺便维护( x )(> b 的数的个数 - < b 的数的个数). 然后从pos向左扫一遍, 假设到 t 点, cnt = [ t, pos ) 内> b 的数的个数 - < b 的数的个数, 那 t 点对答案的贡献为cnt * h(-cnt).累加起来就OK了
--------------------------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream> #define rep(i, n) for(int i = 0; i < n; i++)#define clr(x, c) memset(x, c, sizeof(x))#define h(x) h[(x) + maxn] using namespace std; const int maxn = 100009; int h[maxn << 1], seq[maxn]; int main() { freopen("test.in", "r", stdin); clr(h, 0); int n, b, pos, p = 0; cin >> n >> b; rep(i, n) { scanf("%d", seq + i); if(b == seq[i]) pos = i; } for(int i = pos + 1; i < n; ++i) seq[i] > b ? h(++p)++ : h(--p)++; h(0)++; long long ans = h(0); p = 0; for(int i = pos - 1; i >= 0; i--) { seq[i] < b ? ++p : --p; ans += h(p); } cout << ans << "\n"; return 0;}--------------------------------------------------------------------------------------
1303: [CQOI2009]中位数图
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1626 Solved: 1059
[Submit][Status][Discuss]
Description
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。Input
第一行为两个正整数n和b ,第二行为1~n 的排列。Output
输出一个整数,即中位数为b的连续子序列个数。Sample Input
7 45 7 2 4 3 1 6
Sample Output
4HINT
第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}N<=100000
Source
相关文章推荐
- android LinearLayout设置selector不起作用解决
- LeetCode学习笔记
- 50个强大的Python模块
- UVa 10623 - Thinking Backward (平面图的欧拉公式)
- 00-自测3. 数组元素循环右移问题 (20)
- UVA11426 GCD - Extreme (II) 小于n的数对的gcd之和
- LeetCode 7 Reverse Integer 这道题被血虐
- TCP三次握手详解!
- 杭电1860 统计字符
- scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)
- ueditor使用注意事项
- Web前端开发基础 第四课(CSS小技巧)
- 点击UITableView的cell展开收缩
- 人教版九年级数学的“词汇”中英文对照表翻译bug
- Hadoop2.5.2+ubuntu14.04+eclispe+hadoop2x-eclipse-plugin-master搭建开发环境
- 00-自测2. 素数对猜想 (20)
- 机房收费系统之思路
- POJ 1185:炮兵阵地
- 10 个你必须掌握的超酷 VI 命令技巧
- POJ 1185:炮兵阵地