n ACM/ICPC contest, the ”Dirt Ratio” of a team is calculated in the following way. First let’s ignore all the problems the team didn’t pass, assume the team passed X problems during the contest, and submitted Y times for these problems, then the ”Dirt Ratio” is measured as X/Y. If the ”Dirt Ratio” of a team is too low, the team tends to cause more penalty, which is not a good performance.

Little Q is a coach, he is now staring at the submission list of a team. You can assume all the problems occurred in the list was solved by the team during the contest. Little Q calculated the team’s low ”Dirt Ratio”, felt very angry. He wants to have a talk with them. To make the problem more serious, he wants to choose a continuous subsequence of the list, and then calculate the ”Dirt Ratio” just based on that subsequence.

Please write a program to find such subsequence having the lowest ”Dirt Ratio”.

Input
The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.

In each test case, there is an integer n(1≤n≤60000) in the first line, denoting the length of the submission list.

In the next line, there are n positive integers a1,a2,…,an(1≤ai≤n), denoting the problem ID of each submission.

Output
For each test case, print a single line containing a floating number, denoting the lowest ”Dirt Ratio”. The answer must be printed with an absolute error not greater than 10−4.

Sample Input
1
5
1 2 1 2 3

Sample Output
0.5000000000
Hint

For every problem, you can assume its final submission is accepted.

思路:
这里讲一下我对官方题解以及标程的理解;
1,首先二分答案,来逼近答案;
2,列出求解式子:size(l,r)/(r-l+1)<=mid
size(l,r)为区间(l,r)中不同的数字个数也就是这个区间中ac的题目数量;
mid是二分的结果概率;
这个式子可以转化为:size(l,r)+mid*l<=(r+1)*mid;
那么接下来我们枚举区间右端点从1到n
在线段树中维护size(l,r)+mid*l的最小值;
在线段树查询的时候有技巧,详见代码,这样做可以在查询的时候顺便枚举了区间左端点,这样就是枚举了整个区间;
至于区间右端点从r到r+1我们就需要更新size(l,r)其实也就是在a[i]上一次出现位置的后一个位置到当前的位置,这个区间内更新加一;(因为我们枚举的是区间右端点)

//标程
#include<cstdio>
const int N=60010,M=131100;
int Case,n,i,a[N],ap[N],tag[M];
double v[M],t,L,R,MID;
inline double min(double a,double b)
{return a<b?a:b;
}
inline void tag1(int x,int p)
{tag[x]+=p;v[x]+=p;
}
inline void pb(int x)
{if(tag[x])tag1(x<<1,tag[x]),tag1(x<<1|1,tag[x]),tag[x]=0;
}
void build(int x,int a,int b)
{v[x]=MID*a,tag[x]=0;//初始时size()为0if(a==b)return;int mid=(a+b)>>1;build(x<<1,a,mid),build(x<<1|1,mid+1,b);
}
void change(int x,int a,int b,int c,int d)
{if(c<=a&&b<=d){tag1(x,1);return;}pb(x);int mid=(a+b)>>1;if(c<=mid)change(x<<1,a,mid,c,d);if(d>mid)change(x<<1|1,mid+1,b,c,d);v[x]=min(v[x<<1],v[x<<1|1]);
}
void ask(int x,int a,int b,int d)
{if(b<=d)//只要这个查询区间的右端点小于目前枚举的右端点即可记录答案//这样可以不必一一枚举区间的左端点{if(t>v[x])t=v[x];return;}pb(x);int mid=(a+b)>>1;ask(x<<1,a,mid,d);if(d>mid)ask(x<<1|1,mid+1,b,d);
}
int main()
{scanf("%d",&Case);while(Case--){scanf("%d",&n);for(i=1; i<=n; i++)scanf("%d",&a[i]);L=0,R=1;for(int _=20; _; _--)//二分的次数,保证精度{MID=(L+R)/2;build(1,1,n);//ap[i]为i上一次出现的位置坐标for(i=1; i<=n; i++)ap[i]=0;for(i=1; i<=n; i++){change(1,1,n,ap[a[i]]+1,i);t=1e9;ask(1,1,n,i);if(t-MID*(i+1)<=0)break;ap[a[i]]=i;}if(i<=n)R=MID;else L=MID;}printf("%.10f\n",(L+R)/2);}return 0;
}

hdu6070 Dirt Ratio相关推荐

  1. 【二分】【线段树】hdu6070 Dirt Ratio

    size(l,r)表示区间l,r权值的种类数,让你求min{size(l,r)/(r-l+1)}(1<=l<=r<=n). last[r]表示a[r]上一次出现的位置, 就是二分验证 ...

  2. HDU - 6070 Dirt Ratio (二分 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题目大意:给定一个序列a,对于任何一个区间 [l,r],它的"Dirt Ratio&q ...

  3. HDU 6070 Dirt Ratio(线段树、二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=6070 题解 首先不难看出错误率是单调的,那么我们可以直接二分答案x,某个区间的错误率=区间数的种类cnt/区间长 ...

  4. 2017 Multi-University Training Contest - Team 4:1004. Dirt Ratio

    题意:给你n个数,其中每个区间的权值就是这个区间内不同数的个数除以区间长度,求最小的权值 令size(l, r)表示区间[l, r]内不同数的个数,那么就是要求min(size(l, r)/(r-l+ ...

  5. HDU 6070 Dirt Ratio

    暑假多校赛的一道题,印象深刻,今天终于补了,现在感觉也不是特别难,题意是很经典的那种问题,就是给你一个数列,问一个区间不同的个数比区间的长度的值,在这个数列里的最小值.之前搜过区间不同数的个数的查询问 ...

  6. 有趣题目和认知合集(持续更新)

    写写对一些算法的理解,挂几个有意思的题,可能也会挂几个板子题 算法理解偏向于能懂即可,没有严格的证明 快乐几何 [1.2]Volatile Kite 点到直线 快乐搜与暴力 [2.4]Short Co ...

  7. 2017 Multi-University Training Contest 4 solutions BY 陈松杨

    2017 Multi-University Training Contest 4 solutions BY 陈松杨 发表回复 1001. Big Integer 如果知道k-1k−1个数的个数c_1, ...

  8. 2019 CCSU GOLD!!!

    线段树专场 更新结点,更新区间,区间求和(平均数)+ 树链剖分 51Nod 1199 Money out of Thin Air 更新结点,区间最值,结点查找,区间求和 51Nod 1364 最大字典 ...

  9. 【OpenCV】图像/视频相似度测量PSNR( Peak signal-to-noise ratio) and SSIM,视频/图片转换

    目录 1 目标 2 原理 2.1 图像比较 - PSNR and SSIM¶ 3 代码 3.1如何读取一个视频流(摄像头或者视频文件)?¶ 3 运行效果 视频/图片转换: 如何用OpenCV创建一个视 ...

  10. seaborn使用jointplot函数为散点图添加边缘图、添加回归线、为边缘直方图添加密度曲线、使用ratio函数突出显示边缘图形(focus on Marginal Plot )

    seaborn使用jointplot函数为散点图添加边缘图.添加回归线.为边缘直方图添加密度曲线.使用ratio函数突出显示边缘图形(Make Marginal Plot with focus on ...

最新文章

  1. 对现有代码的分析方法随想
  2. confluence 启动失败的检查思路
  3. SharePoint 搜索功能失效
  4. Android 条码扫描程序源码
  5. java ffmpeg 获取视频时长_Java通过调用FFMPEG获取视频时长
  6. java编程启蒙_程序设计入门—Java语言
  7. python编辑器文字放大_python学习笔记000
  8. CSDN会员免费拿,实现CSDN会员自由的机会到了!!!
  9. SpringBoot整合Shiro框架
  10. matlab 2020b linux版本 下载
  11. 人工智能十大流行算法,通俗易懂讲明白
  12. 电脑记事本增强版notepad++
  13. “卫士通”杯首届四川省高校网络攻防大赛文档及题目
  14. AI 算法工程师面试高频 100 题(附答案详解)
  15. 【总结】1292- 分享几个 VSCode 的高级调试与使用技巧
  16. Python之列表和元组
  17. PTB-XL大型的心电图数据集
  18. C++超市商品管理系统设计最新版
  19. 淘宝新店一个流量没有如何是好
  20. 利用串口解析AIS接收机数据

热门文章

  1. 矩形类的定义(java)
  2. 集腋成裘-15-MongoDB系列-02Find详细
  3. 在EXCEL中生成服从三角分布随机数的方法
  4. 百度推广怎么调整计算机优先,百度竞价优化关于帐户层级的一些设置方法与技巧...
  5. Pillow图像处理
  6. 安装Pillow指定版本出错
  7. xbox360使用_从Xbox360浏览网页
  8. 词类与句子成分对应关系 —— 状语篇
  9. Keil C语言 宏重复定义 问题分析与处理
  10. python,检测代理ip是否有效