Vijos P1549 中位数(CQOI2009)题解
来源:互联网 发布:linux locate which 编辑:程序博客网 时间:2024/05/01 20:34
水题一道,只要知道了一些数学基本知识,就可以了,没有代码难度。
http://baike.baidu.com/link?url=_TwVw9xyLp_T2vJ7lQLWjb6b4UfSI5HYpvN-AQLSHrJ_QS7cyOzUpGbE4102tKoJ8WA0gXTQ58ZmwUkWrXdYKK 中位数的定义,恩…
不难发现:假设 k 在第 pos 个,往左(也就是pos-1 ~ 1)数,当<k的数的数量与>k的数量相同时,该子序列中位数为k,同理,往右(pos+1 ~ n)也是一样的。 那两边呢?我们可以用一个数组来记录出现过的情况,如下列数据:5 41 2 3 4 5 4左一有1个小于4,右1有一个大于4,可以为一个子序列。 附上代码:
#include <vector>#include <list>#include <limits.h>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <cstring>#include <memory.h>using namespace std; //头文件是模板……请忽略……int n,b,num[100010],pos,ans;void init(){ ans = 0; memset(num,0,sizeof(num)); scanf("%d%d",&n,&b); for (int i = 1; i <= n; ++ i) { scanf("%d",&num[i]); if (num[i] == b) pos = i; //pos记录b的位置 }}void work(){ int pos_left[100010],neg_left[100010],pos_right[100010],neg_right[100010],temp; //pos是正数的意思,neg是负数,若temp的值为正数,那小于的数更多 memset(pos_left,0,sizeof(pos_left)); memset(neg_left,0,sizeof(neg_left)); memset(pos_right,0,sizeof(pos_right)); memset(neg_right,0,sizeof(neg_left)); neg_left[0] = neg_right[0] = 1;//自己一个元素{b}也是个序列 temp = 0; for (int i = pos - 1; i >= 1; -- i) { if (num[i] < b) ++ temp; else -- temp; if (temp > 0) ++ pos_left[temp]; else ++ neg_left[-temp]; } temp = 0; for (int i = pos + 1; i <= n; ++ i) { if (num[i] < b) ++ temp; else -- temp; if (temp > 0) ++ pos_right[temp]; else ++ neg_right[-temp]; } ans += neg_left[0] * neg_right[0]; for (int i = 1; i <= n; ++ i) ans += neg_left[i] * pos_right[i] + neg_right[i] * pos_left[i];}int main(){ init(); work(); printf("%d",ans); return 0;}
方法粗暴,简单有效!
0 0
- Vijos P1549 中位数(CQOI2009)题解
- Vijos P1521 跳舞(CQOI2009)题解
- 【CQOI2009】中位数
- CQOI2009中位数
- BZOJ 1303 [CQOI2009] 中位数图 题解与分析
- BZOJ 1303 - [CQOI2009]中位数图 (技巧)
- bzoj 1303: [CQOI2009]中位数图 (乱搞)
- BZOJ_P1303 [CQOI2009]中位数图(中位数)
- [BZOJ1303][CQOI2009]中位数图
- 1303: [CQOI2009]中位数图
- [BZOJ1303][CQOI2009]中位数图
- BZOJ1303[CQOI2009]中位数图
- [BZOJ1303] [CQOI2009]中位数图
- bzoj1303: [CQOI2009]中位数图
- bzoj1303[CQOI2009]中位数图
- Problem : [CQOI2009]中位数图
- [CQOI2009] 中位数图
- [bzoj1303][CQOI2009]中位数图
- 领域模型、贫血模型、充血模型概念总结
- 第二十~章 Functions and Generators
- 《C++语言基础》实践参考——复数类中的运算符重载(续)
- 文件获取路径
- static关键字
- Vijos P1549 中位数(CQOI2009)题解
- 《C++语言基础》实践参考——Time类中的运算符重载(续)
- 《C++语言基础》实践参考——分数类中的运算符重载(续)
- qemu,kvm,qemu-kvm,xen,libvirt的区别
- getEventDispatcher
- 《C++语言基础》实践参考——我的向量类
- 《C++语言基础》实践参考——方程也是类
- android自定义view粒子效果之雨(not surfaceview)
- Windows 内存详解(六) C# WinForm应用程序降低系统内存占用方法总结