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 ...
随机推荐
- js字节转换、字节格式化函数
有时候在上传附件后需要显示大小,可以选择在后台处理,也可以在前台用js处理. 比如我们想1024MB转换成1GB,那就需要进行转换,这里只是介绍用js进行转换. function bytesToSiz ...
- 在React Native中集成热更新
最近,在项目DYTT集成了热更新,简单来说,就是不用重新下载安装包即可达到更新应用的目的,也不算教程吧,这里记录一下. 1.热更新方案 目前网上大概有两个比较广泛的方式,分别是 react-nativ ...
- HTML+CSS : 笔记整理(2 常规流,BFC,固定定位,z-index)
BFC和常规流的关系是什么:常规流遵循BFC,IFC规则. 定位规则总体来说三种: 常规流,浮动,绝对定位(CSS3里面新加了一种flex) 其中常规流包括BFC,IFC等规则,块级元素一个一排地从上 ...
- Swoole 创建服务
1: 创建TCP 服务器 $serv = new swoole_server(‘127.0.0.1’,9501); 2:创建UDP服务器 $serv = new swoole_server('127 ...
- MyFirstDay(附6篇python亲历面试题)
一直以来都是在看别人写的内容,学习前辈们的经验,总感觉自己好像没有什么值得拿出来分享和交流的知识,最近在准备换工作(python后端开发),坐标上海,2019年3月,半个月面了6家(感觉效率是真不高. ...
- selenium库:自动化测试工具
爬虫中主要用来解决Javascript渲染问题 1.声明浏览器对象: from selenium import webdriver browser = webdriver.浏览器名() 2.访问页面: ...
- Python学习笔记:sqlite3(sqlite数据库操作)
对于数据库的操作,Python中可以通过下载一些对应的三方插件和对应的数据库来实现数据库的操作,但是这样不免使得Python程序变得更加复杂了.如果只是想要使用数据库,又不想下载一些不必要的插件和辅助 ...
- 翻译 | “扩展asm”——用C表示操作数的汇编程序指令
本文翻译自GNU关于GCC7.2.0版本的官方说明文档,第6.45.2小节.供查阅讨论,如有不当处敬请指正…… 通过扩展asm,可以让你在汇编程序中使用C中的变量,并从汇编代码跳转到C语言标号.在汇编 ...
- ISCSI网络存储
ISCSI(iSCSI,Internet Small Computer System Interface) iSCSI技术实现了物理硬盘设备与TCP/IP网络协议的相互结合,使得用户可以通过互联网方便 ...
- 10.bootstrap分页,点击哪个分页号,哪个分页号就active
1.分页,点击哪个分页号,哪个分页号就active <nav> <ul class="pagination"> <li><a href=& ...