多校4 Dirt Ratio(二分+线段树)

题意:

给出n个数,找一段区间使得区间内不同数字个数除以区间长度最小,求这个最小值,\(n<=60000,a_i<=n\)

题解:

二分答案mid,检验是否存在一个区间满足\(\frac{size(l,r)}{r-l+1}\leq mid
​\),也就是\(size(l,r)+mid\times l\leq mid\times (r+1)\)

从左往右枚举每个位置作\(r\),当rr变化为\(r+1\)时,对\(size\)的影响是一段区间加1,线段树维护区间最小值即可。

时间复杂度\(O(n\log n\log w)\)。

二分答案和枚举端点想到了,但是求最小值只会遍历,复杂度太高,没想到还能线段树维护,get新技能了。

从左往右枚举右端点,那么第\(i\)个数的贡献就是\(last[a[i]]+1到i\),第i个数都是有贡献的,然后就用线段树搞搞,这样就降了一维复杂度了。

#include<bits/stdc++.h>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
const int N = 6e4 + 10;
const double eps = 1e-6;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c >'9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48 , c = getchar();
return x;
}
int n;
int c[N],last[N],pre[N];
double s[N<<2],col[N<<2];
void pushup(int rt){
s[rt] = min(s[ls],s[rs]);
}
void pushdown(int rt,int m){
if(col[rt] > eps){
col[ls] += col[rt];
col[rs] += col[rt];
s[ls] += col[rt];
s[rs] += col[rt];
col[rt] = 0;
}
}
void update(int L,int R,double val,int l,int r,int rt){
if(L <= l && R >= r){
s[rt] += val;
col[rt] += val;
return ;
}
pushdown(rt,r - l + 1);
int m = l + r >> 1;
if(L <= m) update(L,R,val,lson);
if(R > m) update(L,R,val,rson);
pushup(rt);
}
double query(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
return s[rt];
}
pushdown(rt,r - l + 1);
int m = l + r >> 1;
double ans = 100000000;
if(L <= m) ans = min(ans,query(L,R,lson));
if(R > m) ans = min(ans,query(L,R,rson));
return ans;
}
bool check(double mid){
memset(s,0,sizeof(s));
memset(col,0,sizeof(col));
for(int i = 1;i <= n;i++){
update(last[i]+1,i,1,1,n,1);
update(i,i,mid * i,1,n,1);
double res = query(1,i,1,n,1);
// printf("%d %.6f\n",i,res);
if(mid * (i + 1) - res > eps) return true;
}
return false;
}
int main(){ int T;
T = read();
while(T--){
n = read();
memset(pre,0,sizeof(pre));
for(int i = 1;i <= n;i++) {
c[i] = read();
last[i] = pre[c[i]];
pre[c[i]] = i;
}
double l = 0,r = 1,ans = r;
while(r - l > eps){
double mid = (l + r) / 2;
if(check(mid)) ans = min(ans,mid),r = mid;
else l = mid;
}
printf("%.6lf\n",ans);
}
return 0;
}

2017 多校4 Dirt Ratio的更多相关文章

  1. HDU 6070 - Dirt Ratio | 2017 Multi-University Training Contest 4

    比赛时会错题意+不知道怎么线段树维护分数- - 思路来自题解 /* HDU 6070 - Dirt Ratio [ 二分,线段树 ] | 2017 Multi-University Training ...

  2. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  3. 2017 Multi-University Training Contest - Team 4 hdu6070 Dirt Ratio

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6070 题面: Dirt Ratio Time Limit: 18000/9000 MS (Ja ...

  4. hdu 6070 Dirt Ratio 线段树+二分

    Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Spe ...

  5. HDU 6070 Dirt Ratio(线段树)

    Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Tot ...

  6. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  7. 2017 多校5 hdu 6093 Rikka with Number

    2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...

  8. 2017 多校5 Rikka with String

    2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...

  9. 2017 多校4 Wavel Sequence

    2017 多校4 Wavel Sequence 题意: Formally, he defines a sequence \(a_1,a_2,...,a_n\) as ''wavel'' if and ...

随机推荐

  1. 【Python3】操作文件,目录和路径

    1.遍历文件夹和文件  Python代码   import os import os.path rootdir = "d:/test" for parent,dirnames,fi ...

  2. PHP开发搭建环境二:开发工具PhpStorm安装、激活以及配置

    关于php的开发工具很多,目前市面上最好用最强大的莫过于PhpStorm这款开发神器了,但是鉴于很多开发者朋友在网站上下载的PhpStorm开发工具不能用,或者使用起来很不方便,笔者把最好用的下载地址 ...

  3. Effective Approaches to Attention-based Neural Machine Translation(Global和Local attention)

    这篇论文主要是提出了Global attention 和 Local attention 这个论文有一个译文,不过我没细看 Effective Approaches to Attention-base ...

  4. photoshop入门笔记1:PS的快捷键

    PS部分快捷键: 1.魔棒的作用:比较快捷的抠图工具,对于一些分界线比较明显的图像,通过魔棒工具可以很快速的将图像抠出,魔棒的作用是可以知道你点击的那个地方的颜色,并自动获取附近区域相同的颜色,使它们 ...

  5. mysql 安装常用命令,卸载不干净等

    安装mysql apt-get install mysql-server apt-get install mysql-client sudo apt-get install libmysqlclien ...

  6. 霍夫圆检测 opencv

    进行霍夫圆变换中有一个API:HoughCircles(). 第五个参数为double类型的minDist(),为霍夫变换检测到的圆的圆心之间的最小距离,即让算法能明显区分的两个不同圆之间的最小距离. ...

  7. talent-aio源码阅读小记(一)

    近来在oschina上看到一个很火的java 即时通讯项目talent-aio,恰巧想了解一下这方面的东西,就阅读了一下项目的源码,这里对自己阅读源码后的一些心得体会做一下备忘,也希望能够对其他项目中 ...

  8. P2985 [USACO10FEB]吃巧克力Chocolate Eating

    P2985 [USACO10FEB]吃巧克力Chocolate Eating 题目描述 Bessie has received N (1 <= N <= 50,000) chocolate ...

  9. hadoop中namenode发生故障的处理方法

    Namenode 故障后,可以采用如下两种方法恢复数据: 方法一:将 SecondaryNameNode 中数据拷贝到 namenode 存储数据的目录: 方法 二: 使用 -importCheckp ...

  10. HTTP的缓存控制

    1.缓存的分类: (1)缓存分为服务端侧(server side,比如 Nginx.Apache)和客户端侧(client side,比如 web browser). (2)服务端缓存又分为 代理服务 ...