【HDU 6070 Dirt Ratio】 二分 & 线段树

来源:互联网 发布:中国知网数据库查重 编辑:程序博客网 时间:2024/05/29 11:41

Dirt Ratio

Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1275 Accepted Submission(s): 568
Special Judge

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.

Source
2017 Multi-University Training Contest - Team 4

题意 : s = size(l,r) / (r - l + 1),求最小的 s ,size(l,r),为 区间 l ~ r 内不同的数的个数

思路 : 对于 s = size(l,r) / (r - l + 1) 有,size(l,r) + s * l= s * (r + 1) ,二分枚举 s,在线段树中维护 size(l,r) + s * l 的最小值,在更新的时候对于同一个数,实际上就是更新 前一次的位置到当前位置的区间

AC代码:

#include<cstdio>#include<algorithm>using namespace std;const int MAX = 6e4 + 10;int a[MAX],p[MAX * 4],w[MAX];double ml,mr,mm,v[MAX * 4],s;void sl(int k,int o) { p[k] += o,v[k] += o; }void pp(int k) { if(p[k]) sl(k << 1,p[k]),sl(k << 1 | 1,p[k]),p[k] = 0; }void bulid(int k,int l,int r){    v[k] = mm * l,p[k] = 0;    if(l == r) return ;    int o = (l + r) >> 1;    bulid(k << 1,l,o),bulid(k << 1 | 1,o + 1,r);}void ch(int k,int L,int R,int l,int r){    if(l <= L && r >= R){        sl(k,1);        return ;    }    pp(k);    int o = (L + R) >> 1;    if(l <= o) ch(k << 1,L,o,l,r);    if(r > o) ch(k << 1 | 1,o + 1,R,l,r);    v[k] = min(v[k << 1],v[k << 1 | 1]);}void xun(int k,int l,int r,int o){    if(o >= r){        s = min(s,v[k]);        return ;    }    pp(k);    int w = (l + r) >> 1;    xun(k << 1,l,w,o);    if(o > w) xun(k << 1 | 1,w + 1,r,o);}int main(){    int T,n;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(int i = 1; i <= n; i++) scanf("%d",&a[i]);        ml = 0,mr = 1.0;        int o = 22;        while(o--){            mm = (ml + mr) / 2;            bulid(1,1,n);            int ok = 1;            for(int i = 1; i <= n; i++) w[i] = 0; //w [i]为 i 前一次出现的位置            for(int i = 1; i <= n; i++){                ch(1,1,n,w[a[i]] + 1,i);                s = 0x3f3f3f3f * 1.0;                xun(1,1,n,i);                if(s - mm * (i + 1) <= 0){                    ok = 0;                    break;                }                w[a[i]] = i;            }            if(ok) ml = mm;            else mr = mm;        }        printf("%.10f\n",mm);    }    return 0;}
原创粉丝点击