hdu6070 Dirt Ratio

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.
hdu6070 Dirt Ratio_第1张图片
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
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 ainline 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()为0
    if(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;
}

你可能感兴趣的:(线段树,暑期多校联合训练赛数据结构试题,数据结构与算法)