2017 多校4 Dirt Ratio
多校4 Dirt Ratio(二分+线段树)
题意:
给出n个数,找一段区间使得区间内不同数字个数除以区间长度最小,求这个最小值,\(n<=60000,a_i<=n\)
题解:
二分答案mid,检验是否存在一个区间满足\(\frac{size(l,r)}{r-l+1}\leq mid
\),也就是\(size(l,r)+mid\times l\leq mid\times (r+1)\)
从左往右枚举每个位置作\(r\),当rr变化为\(r+1\)时,对\(size\)的影响是一段区间加1,线段树维护区间最小值即可。
时间复杂度\(O(n\log n\log w)\)。
二分答案和枚举端点想到了,但是求最小值只会遍历,复杂度太高,没想到还能线段树维护,get新技能了。
从左往右枚举右端点,那么第\(i\)个数的贡献就是\(last[a[i]]+1到i\),第i个数都是有贡献的,然后就用线段树搞搞,这样就降了一维复杂度了。
#include<bits/stdc++.h>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
const int N = 6e4 + 10;
const double eps = 1e-6;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c >'9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48 , c = getchar();
return x;
}
int n;
int c[N],last[N],pre[N];
double s[N<<2],col[N<<2];
void pushup(int rt){
s[rt] = min(s[ls],s[rs]);
}
void pushdown(int rt,int m){
if(col[rt] > eps){
col[ls] += col[rt];
col[rs] += col[rt];
s[ls] += col[rt];
s[rs] += col[rt];
col[rt] = 0;
}
}
void update(int L,int R,double val,int l,int r,int rt){
if(L <= l && R >= r){
s[rt] += val;
col[rt] += val;
return ;
}
pushdown(rt,r - l + 1);
int m = l + r >> 1;
if(L <= m) update(L,R,val,lson);
if(R > m) update(L,R,val,rson);
pushup(rt);
}
double query(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
return s[rt];
}
pushdown(rt,r - l + 1);
int m = l + r >> 1;
double ans = 100000000;
if(L <= m) ans = min(ans,query(L,R,lson));
if(R > m) ans = min(ans,query(L,R,rson));
return ans;
}
bool check(double mid){
memset(s,0,sizeof(s));
memset(col,0,sizeof(col));
for(int i = 1;i <= n;i++){
update(last[i]+1,i,1,1,n,1);
update(i,i,mid * i,1,n,1);
double res = query(1,i,1,n,1);
// printf("%d %.6f\n",i,res);
if(mid * (i + 1) - res > eps) return true;
}
return false;
}
int main(){
int T;
T = read();
while(T--){
n = read();
memset(pre,0,sizeof(pre));
for(int i = 1;i <= n;i++) {
c[i] = read();
last[i] = pre[c[i]];
pre[c[i]] = i;
}
double l = 0,r = 1,ans = r;
while(r - l > eps){
double mid = (l + r) / 2;
if(check(mid)) ans = min(ans,mid),r = mid;
else l = mid;
}
printf("%.6lf\n",ans);
}
return 0;
}
2017 多校4 Dirt Ratio的更多相关文章
- HDU 6070 - Dirt Ratio | 2017 Multi-University Training Contest 4
比赛时会错题意+不知道怎么线段树维护分数- - 思路来自题解 /* HDU 6070 - Dirt Ratio [ 二分,线段树 ] | 2017 Multi-University Training ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017 Multi-University Training Contest - Team 4 hdu6070 Dirt Ratio
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6070 题面: Dirt Ratio Time Limit: 18000/9000 MS (Ja ...
- hdu 6070 Dirt Ratio 线段树+二分
Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Spe ...
- HDU 6070 Dirt Ratio(线段树)
Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Tot ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- 2017 多校5 Rikka with String
2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...
- 2017 多校4 Wavel Sequence
2017 多校4 Wavel Sequence 题意: Formally, he defines a sequence \(a_1,a_2,...,a_n\) as ''wavel'' if and ...
随机推荐
- 即将开始的python之路
准备开始学py 记录一下 加油
- python基础回顾笔记
1.知道了什么是编程语言 2.知道了python.C#.Java都是语言的种类 3.python:有很多种 cpython.pypy.jpython... 4.python的执行方式有两种: 解释器 ...
- IBM Rational Software Architect V9.0安装图解
IBM Rational Software Architect(RSA) -- IBM软件开发平台的一部分 – 是IBM在2003年二月并购Rational以来,首次发布的Rational产品.改进过 ...
- SAPバリアント
SAPバリアント VARI バリアント VARID バリアント一覧 VARIT バリアントテキスト VARIS バリアント割当 TVARV バリアント変数(クライアント非依存) TVARVC バリ ...
- PHP.27-TP框架商城应用实例-后台4-使用Gii生成品牌表的代码
Gii安装[GII适用于商城项目] 将Gii文件夹复到application 是,访问http://xx.com/index.php/gii Gii规则[Gii使用规则与建表规则密切相关] 1.建表字 ...
- 18 Django-组件拾遗
一 Django的form组件 forms组件 二 Django的model form组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的 ...
- android 文件下载 超简单
public void downloadPlug(String downloadUrl,String savePath) { try { URL url = new URL(downloadUrl); ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- USACO Section1.4 Mother's Milk 解题报告
milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 【Lowest Common Ancestor of a Binary Search Tree】cpp
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...