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
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
题意
求区间数字种类的个数/区间长度的最大值。
想法
假设结果为mid,求mid= sizer−l+1 的最值,可以转化为求size+l*mid=(r+1)*mid。用线段树保存size(l,r)+mid*l,枚举mid,二分来自做。
#include<bits/stdc++.h>
using namespace std;
const int maxn=60010;
//const double acc=0.0001;
int num[maxn<<2],pos[maxn],a[maxn];
double v[maxn<<2];
double MID,tmp,L,R;
void build(int x,int l,int r)
{//建树
v[x]=MID*l;//l是每次枚举的左端点,相当于mid*l是已知的
num[x]=0;
if(l==r)
return;
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
}
void add(int x,int cnt)
{//单点更新
v[x]+=cnt;
num[x]+=cnt;
}
void pushdown(int x)
{//对子节点进行延迟标记
if(num[x])
{
add(x<<1,num[x]);
add(x<<1|1,num[x]);
num[x]=0;
}
}
void update(int x,int l,int r,int pre,int right)
{//更新每个点
if(pre<=l&&r<=right)
{
add(x,1);//这道题在区间内未出现过 所以贡献一次
return;
}
pushdown(x);
int mid=(l+r)>>1;
if(pre<=mid)
update(x<<1,l,mid,pre,right);
if((mid+1)<=right)
update(x<<1|1,mid+1,r,pre,right);
v[x]=min(v[x<<1],v[x<<1|1]);
}
void query(int x,int l,int r,int right)
{
//size-mid*l<=(r+1)*mid
if(r<=right)
{
if(tmp>v[x])
tmp=v[x];
return;
}
int mid=(l+r)>>1;
query(x<<1,l,mid,right);
if((mid+1)<=right)
query(x<<1|1,mid+1,r,right);
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int k=1; k<=n; ++k)
scanf("%d",&a[k]);
L=0.0,R=1.0;
for(int j=1; j<=24; ++j)
{
MID=(L+R)/2;
build(1,1,n);
int i;
for(i=1; i<=n; ++i)
pos[i]=0;///有问题 下标
for(i=1; i<=n; ++i)
{
tmp=0x3f3f3f3f;//初始化最大值
update(1,1,n,pos[a[i]]+1,i);
query(1,1,n,i);
if(tmp-MID*(i+1)<=0)//这里就是公式的转化,size+MID*l-MID*(r+1)
break;
pos[a[i]]=i;
}
if(i<=n)R=MID;
else L=MID;
}
printf("%.10f",(L+R)/2);
}
return 0;
}
ps:比较匆忙,比赛完再补一下。而且代码还不是优美的,丑哭QAQ