【线段树+二分答案+分数规划】 HDU - 6070 D - Dirt Ratio

D - Dirt Ratio 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.

题目中跟你说误差不超过1e-4,但其实这道题的答案可以是定值,这个时候要想到二分答案

设dp[l][r]为l-r区间内不同数的个数,那么这道题的式子变成了

dp[l][r]/(r-l+1)<=x ----->dp[l][r]+lx-x<=rx

将等式左边的式子建立线段树,x是0-1的二分答案

每次二分,都是以这个x建一次新的线段树

然后跑一遍有端点,更新最小值,并查询是否满足条件

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const double eps=1e-5;
const double INF=999999.0;
double ans;
int pre[maxn],pos[maxn];
int n;

double lazy[maxn*4],tree[maxn*4];

void pushup(int rt)
{
    tree[rt]=min(tree[2*rt],tree[2*rt+1]);
}

void pushdown(int rt)
{
    lazy[2*rt]+=lazy[rt];
    lazy[2*rt+1]+=lazy[rt];
    tree[2*rt]+=lazy[rt];
    tree[2*rt+1]+=lazy[rt];
    lazy[rt]=0;
}

void build(double x,int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        tree[rt]=1.00*(l*x-x);
        return;
    }
    int mid=(l+r)/2;
    build(x,l,mid,2*rt);
    build(x,mid+1,r,2*rt+1);
    pushup(rt);
}

void update(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        tree[rt]+=1;
        lazy[rt]+=1;
        return;
    }
    pushdown(rt);
    int mid=(l+r)/2;
    if(L<=mid) update(L,R,l,mid,2*rt);
    if(R>mid) update(L,R,mid+1,r,2*rt+1);
    pushup(rt);
}

double Query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return tree[rt];
    }
    pushdown(rt);
    int mid=(l+r)/2;
    //ans更新最小值
    if(L<=mid) ans=min(ans,Query(L,R,l,mid,2*rt));
    if(R>mid) ans=min(ans,Query(L,R,mid+1,r,2*rt+1));
    pushup(rt);
    return ans;
}

bool check(double x) //check x 是否是答案,就以此答案为基础建线段树
{
    build(x,1,n,1);
    for(int i=1;i<=n;i++)
    {
        update(pre[i]+1,i,1,n,1); //一定是这个数的前一个位置+1到这个数的所有都要+1
        ans=INF; //每轮Query的初始答案
        if(Query(1,i,1,n,1)<=1.00*i*x) //只要1-i中有区间满足条件,就代表x可以
            return 1;
    }
    return 0;
}

void work() //二分答案找最小
{
    double l=0,r=1.0;
    while(r-l>eps)
    {
        double mid=(l+r)/2;
        if(check(mid)) r=mid;
        else l=mid;
    }
    printf("%.10f\n",l);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(pos,0,sizeof(pos));
        memset(pre,0,sizeof(pre));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            pre[i]=pos[x]; //保留这位数前一次出现的位置
            pos[x]=i;
        }
        work();
    }

    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值