51nod 1682 中位数计数
来源:互联网 发布:android sdk mac 下载 编辑:程序博客网 时间:2024/05/18 02:52
一看数据,O(n^2)可过
我是先向右找,边找边统计有多少个区间能够使当前位数字为中位数,同时记录在每个区间 比当前数字小的数的个数-比当前数字大的数的个数 所得到值的个数。统计完后继续向左统计,统计有多少个区间符合条件,然后同时加上 比当前数字大的数的个数-比当前数字小的数的个数 所得值的个数。
假设当前数字为x,如果左边lt个数字 > x,rt个数字 < x , 右边 zz个数字 > x,yy个数字 < x,若lt-rt=yy-zz,则这个区间当前数字是中位数。
#include <bits/stdc++.h>using namespace std;const int MAXN = 8e3+10;const int M = 8e3+2;int num[MAXN];int mark[MAXN*2];int n;int calc(int index){ memset(mark,0,sizeof(mark)); int f = index+1,b = index-1; int lt = 0,rt = 0; int ret = 1; for(int i = f; i < n; ++i) { if(num[i] < num[index]) ++lt; else ++rt; if(lt == rt) { ++ret; mark[M]++; } else mark[lt-rt+M]++; } lt = rt = 0; for(int i = b; i >= 0; --i) { if(num[i] < num[index]) ++lt; else ++rt; if(lt == rt) ret += 1+mark[M]; else ret += mark[rt-lt+M]; } return ret;}int main(){ scanf("%d",&n); for(int i = 0; i < n; ++i) scanf("%d",&num[i]); for(int i = 0; i < n; ++i) printf("%d ",calc(i)); return 0;}
阅读全文
0 0
- 51nod-1682 中位数计数
- 51nod 1682 中位数计数
- 51NOD 1682 中位数计数
- 51nod-1682 中位数计数
- 51nod 1682 中位数计数
- 51nod 1682 中位数计数
- 51Nod-1682-中位数计数
- 51nod 1682 中位数计数【想法题】
- 51 nod 1682 中位数计数(前缀和)
- 51nod 1682-中位数计数(前缀和)
- 51Nod1682 中位数计数【中位数】
- 51Nod-1645-中位数变换
- 集合计数 51Nod
- 中位数计数
- 中位数计数
- 中位数计数
- HDU5701 中位数计数【中位数】
- 51nod1682:中位数计数(前缀和)
- opencv3 背景建模提取前景图像
- oralce kill 杀不掉进程
- 【IntelliJ IDEA】快捷键
- 快速一键部署测试环境,降低环境配置风险
- java实现socket代理服务器的一些工具设置
- 51nod 1682 中位数计数
- GET和POST的区别
- 关于写adapter适配器的一些经验教训(2017.9.15)
- IntelliJ IDEA 编译方式介绍及编译器的设置和选择
- Python常用学习库汇总
- Dubbo配置含义
- statement和preparedstatement的区别
- C++ 编程出错的地方(考试选择题易错点)
- Sass基础--入门