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.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
15
1 2 1 2 3
Sample Output
0.5000000000Hint
For every problem, you can assume its final submission is accepted.
Source
2017 Multi-University Training Contest - Team 4
题意:给出一段长为n的序列,对于每个子序列,x为出现不同数字的个数,y为序列长度,求出最小的x/y.
题解:二分答案mid,检验是否存在一个区间满足size(l,r)/(r-l+1)<=mid,也就是size(l,r)+mid* l<= mid* (r+1)。
从左往右枚举每个位置作为r,当r变化为r+1时,对size的影响是一段区间加1,线段树维护区间最小值即可。
时间复杂度O(n\log n\log w)。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#define INF 1e9
#define maxn 60005
using namespace std;
int n;
double t[maxn*4];//线段树
int a[maxn];
int last[10];//每个数字最后出现的位置
double add[maxn*4];//记录区间整体加的值
double update(int a,int b,int k,int l,int r,double x,double ad)
{
if(a>r||b<l)
{
t[k]+=ad;
add[k]+=ad;
return t[k];
}
if(a<=l&&b>=r)
{
add[k]+=x+ad;
t[k]+=x+ad;
return t[k];
}
ad+=add[k];
int mid=(l+r)/2;
double s=update(a,b,2*k,l,mid,x,ad);
double ss=update(a,b,2*k+1,mid+1,r,x,ad);
if(s==0) t[k]=ss;//等于0时,不计入最小值
else if(ss==0) t[k]=s;
else t[k]=min(s,ss);
add[k]=0;
return t[k];
}
int query(int a,int b,int k,int l,int r,double x,double ad)
{
if(a<=l&&b>=r)
{
if(t[k]+ad<=x) return 1;
else return 0;
}
ad+=add[k];
int mid=(l+r)/2;
if(a<=mid&&b>mid)
return max(query(a,b,2*k,l,mid,x,ad),query(a,b,2*k+1,mid+1,r,x,ad));
else if(a<=mid&&b<=mid)
return query(a,b,2*k,l,mid,x,ad);
else if(a>mid&&b>mid)
return query(a,b,2*k+1,mid+1,r,x,ad);
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
double l=0,h=1;
while(h-l>1e-5)
{
double mid=(h+l)/2;
memset(t,0,sizeof(t));
memset(last,0,sizeof(last));
memset(add,0,sizeof(add));
int flag=0;
for(int i=1;i<=n;i++)
{
update(last[a[i]]+1,i,1,1,n,1,0);
update(i,i,1,1,n,mid*i,0);
double s=mid*(i+1);
if(query(1,i,1,1,n,s,0)==1)
{
flag=1;
break;
}
last[a[i]]=i;
}
if(flag==1) h=mid;
else l=mid;
}
printf("%.10lf\n",h);
}
return 0;
}