hdu 6070 二分+线段树维护

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 XX problems during the contest, and submitted YY times for these problems, then the ”Dirt Ratio” is measured as XYXY. 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)T(1≤T≤15), denoting the number of test cases.

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

In the next line, there are nn positive integers a1,a2,…,an(1≤ai≤n)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−410−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.

题意:让你统计 size(l,r)/(r-l+1) 这个比值最大,二分最小值,判成立,如何判。把式子化简下即可
size(l,r)+mid*l- mid*(r+1)<=0 l,r未知,所以枚举其中一个变量,很明显是枚举r变量,然后左边的就可以用一个线段树维护了,和上一道的cf的dp+线段树的维护方式很像,所以r变量每次+1的时候,对整体 size(l,r)+mid*l 的影响是 对上一个 a[i] 的位置+1 到右边界所有的权值+1,然后最后找r边界前面的最小值即可,这些用线段树都是log解决的 所以最后的复杂度是 n*logn*logw

#include <bits/stdc++.h>
using namespace std;
const int N = 6e4+100;
double sum[N<<2],add[N<<2];
int pre[N],a[N];
const double eps=1e-4;
double ss;

void pb(int rt)
{
    if(add[rt])
    {
    add[rt<<1]+=add[rt];
    add[rt<<1|1]+=add[rt];
    sum[rt<<1]+=add[rt];
    sum[rt<<1|1]+=add[rt];
    add[rt]=0;
    }
}

void pp(int rt)
{
    sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
}

void build(int l,int r,int rt,double MID)
{
    add[rt]=0;
    if(l==r)
    {
        sum[rt]=l*MID;
        return ;
    }
    int mm=(l+r)>>1;
    build(l,mm,rt<<1,MID);
    build(mm+1,r,rt<<1|1,MID);
    pp(rt);
}

void change(int l,int r,int L,int R,int rt)
{
    if(L<=l&&R>=r)
    {
        add[rt]++;
        sum[rt]++;
        return ;
    }
    pb(rt);
    int mm=(l+r)>>1;
    if(L<=mm) change(l,mm,L,R,rt<<1);
    if(R>mm) change(mm+1,r,L,R,rt<<1|1);
    pp(rt);
}

double ask(int l,int r,int d,int rt)
{
    if(r<=d){
        return sum[rt];
    }
    pb(rt);
    double maxx=1e9;
    int mm=(l+r)>>1;
    maxx=min(maxx,ask(l,mm,d,rt<<1));
    if(d>mm)
        maxx=min(maxx,ask(mm+1,r,d,rt<<1|1));
    return maxx;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        memset(add,0,sizeof(add));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        double l=0,r=1;
        double ou=0.0;
        for(int i=1;i<=20;i++)
        {
            double mid=(l+r)/2.0;
            for(int j=1;j<=n;j++)
                pre[j]=0;
            build(1,n,1,mid);
            int ff=0;
            for(int j=1;j<=n;j++)
            {
                change(1,n,pre[a[j]]+1,j,1);

                if(ask(1,n,j,1)-(j+1)*mid<=eps){
                    ff=1;
                    break;
                }
                pre[a[j]]=j;
            }
            if(ff) r=mid,ou=mid;
            else l=mid;
        }
        printf("%f\n",ou);
    }
}
posted @ 2017-09-19 21:18  黑码的博客  阅读(83)  评论(0编辑  收藏  举报