题目链接

Problem Description

In 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 XY. If the ''Dirt Ratio'' of a team is too low, the team tends to cause more penalty, which is not a good performance.

Picture from MyICPC

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

题意:

在给出的数列里面寻找一段区间使得区间内不同数的个数/区间长度的比值最小,输出这个最小值。

分析:把可能的结果二分,然后用线段树求解

如果我们设sum为一个区间内不同数的个数,len为这个区间长度

我们先二分答案得到k,每次判断这个答案k是否是我们要找的答案。那么我们需要在序列中找一段区间使得它的sum/len<=k转换一下得到sum-lenk<=0,我们每次判断这个区间之内的这个条件是否成立。

现在问题就很好解决了,sum可以利用线段树解决:从左往右插入数字,设A[i]上一次出现的位置为pre[i],那么[pre[i]+1,i]这一段权值加1,sum[j]表示的是:区间[j,i]内不同数的个数,这样从左往右插入数字后,所有的区间都被枚举过了,那么还剩下len
k,这个只要每插入一个数A[i],就把[1,i]的权值都减去k即可。

#include<iostream>
#include<stdio.h>
using namespace std;
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
const int max_n=6e4+10;
int n,a[max_n],last[max_n],pre[max_n];///last[i]表示i这个值最后出现的位置,pre[i]表示i这个位置上的数值上次出现的位置
double sum[max_n << 2], add[max_n << 2];///sum表示的是一个区间之内的和,add起一个中间转换的作用
void push_down(int root)///向下更新左右子树的节点的值
{
sum[root<<1]+=add[root];
sum[root<<1|1]+=add[root];
add[root<<1]+=add[root];
add[root<<1|1]+=add[root];
add[root]=0;
} void push_up(int root)///根据左右子树向上更新根节点的值
{
sum[root]=min(sum[root<<1],sum[root<<1|1]);
} void build(int left,int right,int root)///建树时每个节点的sum和add都是0(包括最下层的叶子节点)
{
sum[root]=0;
add[root]=0;
if(left==right)
return ;
int mid=(left+right)>>1;
build(lchild);
build(rchild);
push_up(root);
} void update(int l,int r,double w,int left,int right,int root)
///[l,r]是需要更新的区间,[left,right]是每次二分的区间
{
if(l<=left&&r>=right)///在整个的区间之内
{
add[root]+=w;
sum[root]+=w;
return ;
} push_down(root);///向下更新 int mid=(left+right)>>1;
if(l<=mid) update(l,r,w,lchild);///更新左子树
if(r>mid) update(l,r,w,rchild);///更新右子树
push_up(root);///由左右子树向上更新
} double query(int l,int r,int left,int right,int root)
///[l,r]是需要更新的区间,[left,right]是每次二分的区间
{
if(l<=left&&r>=right) return sum[root];
push_down(root);
int mid=(left+right)>>1;
double ans=n;
if(l<=mid) ans=min(ans,query(l,r,lchild));
if(r>mid) ans=min(ans,query(l,r,rchild));
push_up(root);
return ans;
} bool Find(double m)
{
build(1,n,1);
for(int i=1; i<=n; i++)
{
update(pre[i]+1,i,1,1,n,1);
update(1,i,-m,1,n,1);
if(query(1,i,1,n,1)<=0) return 1;
}
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (int i = 1; i <= n; i++)
last[i] = pre[i] = 0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
pre[i]=last[a[i]];
last[a[i]]=i;
}
double le=0.0,ri=1.0;
for(int i=1; i<20; i++)
{
double mi=(le+ri)/2;
if(Find(mi)) ri=mi;
else
le=mi;
}
printf("%.9lf",ri);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  2. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  3. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  4. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  5. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  6. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  7. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  8. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

  9. 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)

    题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...

随机推荐

  1. DELL服务器PXE前期处理

    thaks:https://www.cnblogs.com/520ZXL/ PXE批量推系统,服务器要具备条件:raid处理好,设置为pxe启动,与PXE服务器网络要通 先进入磁盘阵列(ctrl+R) ...

  2. IDEA设置头注释—自定义author和date

    IDEA设置头注释,自定义author和date的方法如下所示: 去掉波浪线的方式:鼠标选中单词 --> 点击鼠标右键 --> spelling --> save 'xxx' to ...

  3. javascript与python的比较

    1:javascript与python大小写皆敏感 2:javascript使用{}来组织代码块,与大部分语言相同  python使用缩进来组织代码块,与大部分语言不同,请务必遵守约定俗成的习惯,坚持 ...

  4. Delphi通过ADO链接数据库及对数据库的增加,删除,修改,读取操作实例教程4

    ADO是一种程序对象,用于表示用户数据库中的数据结构和所包含的数据.ADO(ActiveXDataObjects,ActiveX数据对象)是Microsoft提出的应用程序接口(API)用以实现访问关 ...

  5. 【bzoj5157】[Tjoi2014]上升子序列 树状数组

    题目描述 求一个数列本质不同的至少含有两个元素的上升子序列数目模10^9+7的结果. 题解 树状数组 傻逼题,离散化后直接使用树状数组统计即可.由于要求本质不同,因此一个数要减去它前一次出现时的贡献( ...

  6. BZOJ4888 Tjoi2017异或和(树状数组)

    化为前缀和相减.考虑每一位的贡献.则需要快速查询之前有几个数和当前数的差在第k位上为1.显然其与更高位是无关的.于是用BIT维护后k位的数的出现次数,瞎算一算即可. // luogu-judger-e ...

  7. C++四种类型转化

    2018-08-02 (星期四)C++类型转换:static_cast提供编译时期静态类型检测:    static_cast <type-id> (expression)    1)完成 ...

  8. 【纪念】NOIP2018后记——也许是一个新的起点

    如果你为了失去太阳而哭泣,那么你也将失去星星和月亮. —— 泰戈尔<飞鸟集> NOIP结束了,我挂了一道题……曾经在心中觉得怎么都不会考到的分数,就这么冷冷的出现在了我的成绩单上.的确是比 ...

  9. BZOJ2277 [Poi2011]Strongbox 【数论】

    题目链接 BZOJ2277 题解 orz太难了 如果一个数\(x\)是密码,那么所有\((x,n)\)的倍数都是密码 如果两个数\(x,y\)是密码,那么所有\((x,y)\)的倍数都是密码 那么如果 ...

  10. XSS/CSRF跨站攻击和防护方案

    Xss(Cross Site Scripting 跨站脚本攻击)/CSRF(Cross-site request forgery 跨站请求伪造),它与著名的SQL注入攻击类似,都是利用了Web页面的编 ...