Dirt Ratio HDU - 6070 (01分数规划)

Dirt Ratio

 HDU - 6070 

 

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 XX problems during the contest, and submitted YY times for these problems, then the ''Dirt Ratio'' is measured as XYXY. 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)T(1≤T≤15), denoting the number of test cases. 

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

In the next line, there are nn positive integers a1,a2,...,an(1≤ai≤n)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−410−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.

   

 

https://www.luogu.org/blog/yestoday/post-01-fen-shuo-gui-hua-yang-xie

https://blog.csdn.net/Davenny/article/details/76650940

思路: 
这里讲一下我对官方题解以及标程的理解; 
1,首先二分答案,来逼近答案; 
2,列出求解式子:size(l,r)/(r-l+1)<=mid 
size(l,r)为区间(l,r)中不同的数字个数也就是这个区间中ac的题目数量; 
mid是二分的结果概率; 
这个式子可以转化为:size(l,r)+mid*l<=(r+1)*mid; 
那么接下来我们枚举区间右端点从1到n 
在线段树中维护size(l,r)+mid*l的最小值; 
在线段树查询的时候有技巧,详见代码,这样做可以在查询的时候顺便枚举了区间左端点,这样就是枚举了整个区间; 
至于区间右端点从r到r+1我们就需要更新size(l,r)其实也就是在a[i]上一次出现位置的后一个位置到当前的位置,这个区间内更新加一;(因为我们枚举的是区间右端点)

我们每次枚举右端点,当新加入一个点的时候,我们需要对不包含这个数的最右的连续区间进行整体加1

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e6 + 7;
const double eps = 1e-7;
double sum[N]; // sum = size(l, r) + l*mid
int lazy[N];  
int pre[60007]; //这个种类上一次出现的位置 
int a[60007]; // i的种类 
int n; 
void build(int l, int r, int rt, double mi) {
	lazy[rt] = 0; //初始种类数为零 
	if (l == r) {
		sum[rt] = l * mi; // l*mid 
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, rt << 1, mi);
	build(mid + 1, r, rt << 1 | 1, mi);
	sum[rt] = min(sum[rt<<1], sum[rt<<1|1]); //size(l, r) + l*mid 最小值 
}
void pushdown(int rt) {
	lazy[rt << 1] += lazy[rt];
	lazy[rt << 1|1] += lazy[rt];
	sum[rt << 1] += lazy[rt];
	sum[rt << 1|1] += lazy[rt];
	lazy[rt] = 0;
}
void update(int l, int r, int rt, int L, int R) { //更新 L ~ R的种类数 
	if (L <= l && r <= R) {
		sum[rt] += 1;
		lazy[rt] += 1;
		return;
	}
	if (lazy[rt]) pushdown(rt);
	int mid = (l + r) >> 1;
	if (L <= mid) update(l, mid, rt<<1, L, R);
	if (R > mid) update(mid+1, r, rt<<1|1, L, R);
	sum[rt] = min(sum[rt<<1], sum[rt<<1|1]);
}
double query(int l, int r, int rt, int L, int R) { //更新 L ~ R的size(l, r) + l*mid 最小值  
	if (L <= l && r <= R) {
		return sum[rt];
	}
	if (lazy[rt]) pushdown(rt);
	int mid = (l + r) >> 1;
	double tmp = 1e9;
	if (L <= mid) tmp = min(tmp, query(l, mid, rt<<1, L, R));
	if (R > mid) tmp = min(tmp, query(mid+1, r, rt<<1|1, L, R));
	return tmp;
}

int check(double mid) {
	build(1, n, 1, mid);
	memset(pre, 0, sizeof(pre));
	for (int i = 1; i <= n; ++i) { //从左到右枚举r 
		//在pre[a[r]]+1~r这段区间内将区间值+1,因为这段区间内a[r]并没有出现过 
		update(1, n, 1, pre[a[i]]+1, i); //从左到右更新种类数 
		//查询以i为R的区间(种类数,如果找到满足的最小值则返回 
		if (query(1, n, 1, 1, i) - mid*(i+1) < eps) return 1; 
		pre[a[i]] = i; //更新最后出现的位置 
	}
	return 0; //找不到能满足的最小值 
}
// size(l,r)/(r-l+1) <= mid 
// size(l,r)+mid*l <= (r+1)*mid; 
// size(l,r)+mid*l - (r+1)*mid <= 0 (eps) 
//在线段树中维护size(l,r)+mid*l的最小值 
int main() {
	int t;
	scanf ("%d", &t);
	while (t--) {
		scanf ("%d", &n);
		for (int i = 1; i <= n; ++i) {
			scanf ("%d", &a[i]);
		}
		double l = 0, r = 1;
		while (r - l > eps) {
			double mid = (l + r)/2.0;
			if (check(mid)) r = mid;
			else l = mid;
		}
		printf ("%0.5f\n", l);
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值