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.
这里写图片描述
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()为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;
}
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值