题意:给你n个数,其中每个区间的权值就是这个区间内不同数的个数除以区间长度,求最小的权值
令size(l, r)表示区间[l, r]内不同数的个数,那么就是要求min(size(l, r)/(r-l+1)) (1<=l<=n)(1<=r<=n)
可以二分答案ans = size(l, r)/(r-l+1)
将式子转化一下:size(l, r)+ans*l=ans*(r+1)
其中size(l, r)+ans*(l-1)很明显可以用线段树维护最小值
然后二分每次判定min(size(l, r)+ans*(l-1))是否小于ans*t就好了
又是道经典题,线段树的处理方式和下面两题几乎一模一样
http://blog.csdn.net/jaihk662/article/details/76559758
http://blog.csdn.net/jaihk662/article/details/76451307
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n, pre[60005], a[60005], pos[60005];
double tre[245424], temp[245424];
void Lazy(int l, int r, int x)
{
int m;
m = (l+r)/2;
tre[x*2] += temp[x];
tre[x*2+1] += temp[x];
if(l!=m)
temp[x*2] += temp[x];
if(r!=m+1)
temp[x*2+1] += temp[x];
temp[x] = 0;
}
void Update(int l, int r, int x, int a, int b, double c)
{
int m;
if(l>=a && r<=b)
{
tre[x] += c;
if(l!=r)
temp[x] += c;
return;
}
if(temp[x])
Lazy(l, r, x);
m = (l+r)/2;
if(a<=m)
Update(l, m, x*2, a, b, c);
if(b>=m+1)
Update(m+1, r, x*2+1, a, b, c);
tre[x] = min(tre[x*2], tre[x*2+1]);
}
double Query(int l, int r, int x, int a, int b)
{
int m;
double ans = 2*n+1;
if(l>=a && r<=b)
return tre[x];
if(temp[x])
Lazy(l, r, x);
m = (l+r)/2;
if(a<=m)
ans = min(ans, Query(l, m, x*2, a, b));
if(b>=m+1)
ans = min(ans, Query(m+1, r, x*2+1, a, b));
return ans;
}
int Jud(double val)
{
int i;
memset(tre, 0, sizeof(tre));
memset(temp, 0, sizeof(temp));
for(i=1;i<=n;i++)
{
Update(1, n, 1, pre[i]+1, i, 1);
Update(1, n, 1, i, i, val*i);
if(Query(1, n, 1, 1, i)<=val*(i+1))
return 1;
}
return 0;
}
int main(void)
{
int T, i;
double l, r, m;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(pos, 0, sizeof(pos));
for(i=1;i<=n;i++)
{
scanf("%d", &a[i]);
pre[i] = pos[a[i]];
pos[a[i]] = i;
}
l = 0, r = 1;
while(r-l>0.00005)
{
m = (l+r)/2;
if(Jud(m))
r = m;
else
l = m;
}
printf("%f\n", r);
}
return 0;
}