(hdu6070)2017杭电多校联赛第四场-Dirt Ratio 线段树+二分

Dirt Ratio

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1119    Accepted Submission(s): 495
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  Xproblems 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(1T15), denoting the number of test cases.

In each test case, there is an integer  n(1n60000) in the first line, denoting the length of the submission list.

In the next line, there are  n positive integers  a1,a2,...,an(1ain), 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  104.
 

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


题目大意:给你一些数据,然后询问在这些数据中找到一段连续的数据使得我们能得到最小的正确率。
正确率=(ac数量)/(提交次数)

解题思路:我们需要得到的是正确率,所以正确率肯定在0~1之间,所以我们可以二分正确率,然后我们假设得到了这个正确率,我们就可以根据每次提交的题目建立线段树,维护的是区间内的题目种类,正确率*线段树长度=题目种类,所以也就可以逆向解决得到我们需要的题目种类。然后我们每次比较得到题目种类和概率得到的种类比较,从而二分概率,具体请看标程。想法很重要。

标程代码:
#include
const int N=60010,M=131100;
int Case,n,i,a[N],ap[N],tag[M];
double v[M],t,L,R,MID;
inline double min(double a,double b) {
	return a>1;
	build(x<<1,a,mid),build(x<<1|1,mid+1,b);
}
void change(int x,int a,int b,int c,int d) {//区间更新 
	if(c<=a&&b<=d) {
		tag1(x,1);
		return;
	}
	pb(x);
	int mid=(a+b)>>1;
	if(c<=mid)change(x<<1,a,mid,c,d);
	if(d>mid)change(x<<1|1,mid+1,b,c,d);
	v[x]=min(v[x<<1],v[x<<1|1]);//回溯更新得到最小的种类值 
}
void ask(int x,int a,int b,int d) {
	if(b<=d) {
		if(t>v[x])t=v[x];
		return;
	}
	pb(x);
	int mid=(a+b)>>1;
	ask(x<<1,a,mid,d);
	if(d>mid)ask(x<<1|1,mid+1,b,d);
}
int main() {
	scanf("%d",&Case);
	while(Case--) {
		scanf("%d",&n);
		for(i=1; i<=n; i++)scanf("%d",&a[i]);
		L=0,R=1;//二分概率值 
		for(int _=20; _; _--) {//保证精度 
			MID=(L+R)/2;
			build(1,1,n);
			for(i=1; i<=n; i++)ap[i]=0;//初始全部赋为0 
			for(i=1; i<=n; i++) {
				change(1,1,n,ap[a[i]]+1,i);
				t=1e9;
				ask(1,1,n,i);
				if(t-MID*(i+1)<=0)break;//种类值变少 
				ap[a[i]]=i;
			}
			if(i<=n)R=MID;// 种类值少即概率要减少 
			else L=MID;
		}
		printf("%.10f\n",(L+R)/2);
	}
	return 0;
}

题目链接:点击打开链接
 

你可能感兴趣的:(杭电随笔,线段树)