Dirt Ratio
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1087 Accepted Submission(s): 475
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
题目大意: 给出一个序列,问所有连续子序列中,种类/长度最小为多少?
解题思路:线段树+二分
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const double eps=1e-10;
const int MAXN=1e5+5;
const int MOD=1e9+7;
int a[MAXN],last[MAXN],pre[MAXN];
int n;
struct segment_tree
{
double val[MAXN*4],lazy[MAXN*4];
void pushup(int rt)
{
val[rt]=min(val[lson],val[rson]);
}
void build(int l,int r,int rt,double M)
{
lazy[rt]=0;
if(l==r) {val[rt]=l*M;return;}
int m=(l+r)>>1;
build(l,m,lson,M);
build(m+1,r,rson,M);
pushup(rt);
}
void pushdown(int rt)
{
lazy[lson]+=lazy[rt];lazy[rson]+=lazy[rt];
val[lson]+=lazy[rt];val[rson]+=lazy[rt];
lazy[rt]=0;
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
lazy[rt]+=c;val[rt]+=c;return;
}
if(lazy[rt]!=0) pushdown(rt);
int m=(l+r)>>1;
if(L<=m) update(L,R,c,l,m,lson);
if(R>m) update(L,R,c,m+1,r,rson);
pushup(rt);
}
double query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r) return val[rt];
if(lazy[rt]!=0) pushdown(rt);
int m=(l+r)>>1;
if(R<=m) return query(L,R,l,m,lson);
else if(L>m) return query(L,R,m+1,r,rson);
else return min(query(L,m,l,m,lson),query(m+1,R,m+1,r,rson));
pushup(rt);
}
bool check(double M)
{
build(1,n,1,M);
for(int i=1;i<=n;++i)
{
update(pre[i]+1,i,1,1,n,1);
if(query(1,i,1,n,1)<=M*(i+1)) return true;
}
return false;
}
};
segment_tree st;
//val=size(l,r)+mid*l
//val<=mid(r+1)
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(pre,0,(n+5)*sizeof(int));
memset(last,0,(n+5)*sizeof(int));
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
pre[i]=last[a[i]];
last[a[i]]=i;
}
double L=0,R=1,M;
for(int i=0;i<20;++i)
{
M=(L+R)/2;
if(st.check(M)) R=M;
else L=M;
}
printf("%.10f\n",L);
}
return 0;
}