BZOJ 1303 CQOI 2009 中位数图 模拟
来源:互联网 发布:php是干什么的 编辑:程序博客网 时间:2024/06/05 04:19
题目大意:给出一个1~n的排列,问这其中的中位数为k的子序列的数量是多少。
思路:要想让k为中位数,一定要让它出现在这个序列中所以就以这个数为中心向两边拓展。想让一个数成为中位数,那么这个序列中出现的小于它的数一定要等于大于它的数。考虑到这个数左右都有可能有数字,那么就可以表示成:前多+后多=前少+后少,也就是前多-前少=后少-后多。之后就用个数组随意统计一下就行了。
CODE:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100010#define BASE 100001using namespace std; int points,pos,p;int src[MAX]; int _less[MAX],more[MAX];int cnt[MAX << 1]; int main(){ cin >> points >> p; for(int i = 1; i <= points; ++i) { scanf("%d",&src[i]); if(src[i] == p) pos = i; } ++cnt[BASE]; for(int i = pos - 1; i; --i) { _less[i] = _less[i + 1],more[i] = more[i + 1]; src[i] < p ? ++_less[i]:++more[i]; } for(int i = pos + 1; i <= points; ++i) { _less[i] = _less[i - 1],more[i] = more[i - 1]; src[i] < p? ++_less[i]:++more[i]; ++cnt[BASE + _less[i] - more[i]]; } int ans = 0; for(int i = pos; i; --i) ans += cnt[BASE + more[i] - _less[i]]; cout << ans << endl; return 0;}
0 0
- BZOJ 1303 CQOI 2009 中位数图 模拟
- [BZOJ 1303] CQOI 2009 中位数图 · 数学
- 【CQOI 2009】 中位数图
- 【CQOI 2009】 中位数
- BZOJ 1303-中位数图(中位数技巧)
- 【模拟】【CQOI 2007】【bzoj 1258】三角形tri
- 【BZOJ 1303】 [CQOI2009]中位数图
- bzoj 1303 [CQOI2009]中位数图
- BZOJ 1303 [CQOI2009]中位数图
- 【BZOJ 1303】 【CQOI2009】中位数图
- BZOJ 1303: [CQOI2009]中位数图
- bzoj 1303: [CQOI2009]中位数图
- BZOJ 1303 [CQOI2009]中位数图
- BZOJ 1303 - [CQOI2009]中位数图 (技巧)
- BZOJ-1303 中位数图 数学+乱搞
- 【BZOJ】1303 [CQOI2009]中位数图 建模
- BZOJ 1303: [CQOI2009]中位数图 计数,思维
- bzoj 1303: [CQOI2009]中位数图 (乱搞)
- 在mvc4里怎样引用:System.Web.Optimization和entityframework
- IOS中http请求的cookie查看,删除,添加
- A potentially dangerous Request.Form value was detected from the client
- 产品经理在研发流程中扮演的角色
- 关于C++中纯虚函数的一个记录
- BZOJ 1303 CQOI 2009 中位数图 模拟
- Android开机启动过程分析
- 互联网产品经理能力体系
- 盘点一些iOS开发技巧:Swift和Objective-C
- JAVA获取CLASSPATH路径
- opencv 3.0 Mat 数据结构 和 运算操作
- 天气预报接口api(中国天气网)
- 大话产品经理:真的 “人人都是产品经理” 吗?
- APP界面设计的几个建议