hdu 6070 Dirt Ratio —— 二分+线段树

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
Hint

For every problem, you can assume its final submission is accepted.
感觉现在的数据结构越来越不单纯了,看来还是得全面发展,这样一个东西我都没有想到:
s u m ( l , r ) / ( l + r 1 ) <= m i d == s u m ( l , r ) + l m i d <= ( r + 1 ) m i d
sum指的是l到r之间有多少不相同的数,然后就for一遍r,居然不会tQAQ

#include<bits/stdc++.h>
using namespace std;
const int maxn=6e4+5;
double sum[maxn*4],flag[maxn*4];
int n;
void pushup(int root)
{
    sum[root]=min(sum[root<<1],sum[root<<1|1]);
}
void pushdown(int root)
{
    if(!flag[root])
        return;
    sum[root<<1]+=flag[root];
    sum[root<<1|1]+=flag[root];
    flag[root<<1]+=flag[root];
    flag[root<<1|1]+=flag[root];
    flag[root]=0;
}
void build(int l,int r,int root,double m)
{
    flag[root]=sum[root]=0;
    if(l==r)
    {
        sum[root]=(double)l*m;
        return ;
    }
    int mid=l+r>>1;
    build(l,mid,root<<1,m);
    build(mid+1,r,root<<1|1,m);
    pushup(root);
}
void update(int l,int r,int root,int ql,int qr,double val)
{
    if(l>=ql&&r<=qr)
    {
        flag[root]+=val;
        sum[root]+=val;
        return ;
    }
    pushdown(root);
    int mid=l+r>>1;
    if(mid>=ql)
        update(l,mid,root<<1,ql,qr,val);
    if(mid<qr)
        update(mid+1,r,root<<1|1,ql,qr,val);
    pushup(root);
}
double query(int l,int r,int root,int ql,int qr)
{
    if(l>=ql&&r<=qr)
        return sum[root];
    int mid=l+r>>1;
    double ans=1e7;
    if(mid>=ql)
        ans=min(ans,query(l,mid,root<<1,ql,qr));
    if(mid<qr)
        ans=min(ans,query(mid+1,r,root<<1|1,ql,qr));
    return ans;
}
int pre[maxn],last[maxn];
int check(double m)
{
    build(1,n,1,m);
    for(int i=1;i<=n;i++)
    {
        update(1,n,1,pre[i]+1,i,1);
        //cout<<i<<" "<<query(1,n,1,1,i)<<endl;
        if(query(1,n,1,1,i)<=(double)m*(i+1))
            return 1;
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(last,0,sizeof(last));
        int x;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            pre[i]=last[x];
            last[x]=i;
        }
        double low=0,high=1,mid;
        while(high-low>=1e-5)
        {
            mid=(low+high)/2;
            if(check(mid)==1)
                high=mid-1e-6;
            else
                low=mid+1e-6;
        }
        printf("%.10f\n",high);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82424751
今日推荐