51nod 1711 平均数(二分 树状数组)
来源:互联网 发布:sqlserver删除历史数据 编辑:程序博客网 时间:2024/06/16 21:54
题意:给你一个长度为n的序列,问所有区间(n*(n+1)/2个区间)中第k大的平均数。
思路:首先可以想到答案具有单调性,所以可以用二分,但是怎么二分呢?
所以我们只要将所有的sum[i]-ans*i作为新数组,类似求逆序对一样的方法插入BIT,就能计算出满足>=ans的个数, 注意别忘了把0也插入进去。因为存在sum[l-1]-ans*(l-1), 还有k别忘了开long long。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int n, a[maxn];int tree[maxn];ll sum[maxn], k;double b[maxn], Hash[maxn];int lowbit(int x){ return x&(-x);}void update(int pos, int val){ while(pos < maxn) { tree[pos] += val; pos += lowbit(pos); }}int query(int pos){ int res = 0; while(pos) { res += tree[pos]; pos -= lowbit(pos); } return res;}ll judge(double x){ memset(tree, 0, sizeof(tree)); for(int i = 0; i <= n; i++) b[i] = Hash[i] = sum[i]-x*i; sort(Hash, Hash+n+1); int d = unique(Hash, Hash+n+1)-Hash; ll cnt = 0; for(int i = 0; i <= n; i++) { int id = lower_bound(Hash, Hash+d, b[i])-Hash+1; cnt += query(id); update(id, 1); } return cnt;}int main(void){ while(cin >> n >> k) { memset(sum, 0, sizeof(sum)); for(int i = 1; i <= n; i++) scanf("%d", &a[i]), sum[i] = sum[i-1]+a[i]; double l = 1, r = 100000, ans; while(r-l >= 1e-6) { double mid = (l+r)/2.0; if(judge(mid) < k) r = mid; else l = mid, ans = mid; } printf("%.3f\n", ans); } return 0;}
阅读全文
1 0
- 51nod 1711 平均数(二分 树状数组)
- 51Nod-1711平均数(二分+树状数组|线段树)
- 51nod 平均数(二分+离散+树状数组)
- [树状数组]51 Nod 1711——平均数
- 51NOD 1711平均数
- 51nod 1711 平均数
- 【51nod 1711】平均数
- 51nod-1711 平均数
- 51Nod-1711-平均数
- 51nod 1711 平均数
- 【二分+正序对】51Nod 1711 平均数
- test 3 Problem A: [noip2016十连测第三场]平均数 (实数二分+排序+树状数组)
- 【51nod】 第K大区间2(二分+树状数组)
- [二分+树状数组]51 Nod 1685——第K大区间2
- 51nod 1685 树状数组+打标记
- 51Nod 1019树状数组离散化
- 51 nod 1272 最大距离(树状数组)
- 51Nod 1081 子段求和(数组/树状数组)
- PHP实现MYSQL简单的增删改查功能
- Linux chsh命令
- 1、HTML 学习记录——简介
- 从0开始学习cuda和caffe源码阅读
- MyBatis 映射器
- 51nod 1711 平均数(二分 树状数组)
- express支持i18n国际化
- Android Camera 正方形预览(二)
- [数论] 51nod 1365 Fib(N) mod Fib(K)
- python练习----基本流程控制
- JQuery lhgdialog的使用
- org.apache.commons.io.FileUtils文件操作解析
- odps词频统计
- jquery easyui combobox required=true设置