hdu 6070 Dirt Ratio
题
OvO http://acm.hdu.edu.cn/showproblem.php?pid=6070
(2017 Multi-University Training Contest - Team 4 - 1004)
解
二分答案
check时,要满足distinct(l,r)/(r-l+1)<val ,将这个不等式转化为distinct(l,r)+val*l<val*(r+1)
check的时候,从左到右枚举右端点r,用线段树维护查询从1到r中选一个l,distinct(l,r)+val*l的最小值
lst数组的作用:lst[i]表示枚举右端点时上一个值为i的点出现的位置
这样的话,线段树的更新就是当枚举到右端点为i的时候,lst[s[i]]+1到i的值全部加1
查询就是查询1到i中的最小值
(思路来源 标程之类的东西)
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- const int M=6e4+55;
- int n;
- int s[M],lst[M];
- double tree[M*3],tag[M*3];
- void build(int rt,int li,int ri,double val)
- {
- tag[rt]=0;
- if(li==ri)
- {
- tree[rt]=val*li;
- return ;
- }
- int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
- build(lc,li,mid,val);
- build(rc,mid+1,ri,val);
- tree[rt]=min(tree[lc],tree[rc]);
- }
- void pushdown(int rt,int li,int ri)
- {
- if(li==ri)
- {
- tag[rt]=0;
- return ;
- }
- int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
- tree[lc]+=tag[rt];
- tree[rc]+=tag[rt];
- tag[lc]+=tag[rt];
- tag[rc]+=tag[rt];
- tag[rt]=0;
- }
- void update(int rt,int li,int ri,int lq,int rq,double val) //add
- {
- if(lq<=li && ri<=rq)
- {
- tag[rt]+=val;
- tree[rt]+=val;
- return ;
- }
- int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
- if(tag[rt])
- pushdown(rt,li,ri);
- if(mid>=lq)
- update(lc,li,mid,lq,rq,val);
- if(mid+1<=rq)
- update(rc,mid+1,ri,lq,rq,val);
- tree[rt]=min(tree[lc],tree[rc]);
- }
- double query(int rt,int li,int ri,int lq,int rq) //get min
- {
- double ret=1e9+7;
- if(lq<=li && ri<=rq)
- return tree[rt];
- int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
- if(tag[rt])
- pushdown(rt,li,ri);
- if(mid>=lq)
- ret=min(ret,query(lc,li,mid,lq,rq));
- if(mid+1<=rq)
- ret=min(ret,query(rc,mid+1,ri,lq,rq));
- return ret;
- }
- bool check(double val)
- {
- int i,j;
- double tmp;
- build(1,1,n,val);
- memset(lst,0,sizeof(lst));
- for(i=1;i<=n;i++)
- {
- update(1,1,n,lst[s[i]]+1,i,1);
- tmp=query(1,1,n,1,i); //tmp=distinct(l,r)+val*l
- if(tmp<val*(i+1)) //distinct(l,r)+val*l<val*(r+1) => distinct(l,r)/(r-l+1)<val
- return true;
- lst[s[i]]=i;
- }
- return false;
- }
- void solve()
- {
- double li=0,ri=1,mid;
- int i,j,cnt=20;
- while(cnt--)
- {
- mid=(li+ri)/2;
- if(check(mid))
- ri=mid;
- else
- li=mid;
- }
- printf("%.10lf\n",ri);
- }
- int main()
- {
- int i,j;
- int cas;
- cin>>cas;
- while(cas--)
- {
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- scanf("%d",&s[i]);
- solve();
- }
- return 0;
- }
hdu 6070 Dirt Ratio的更多相关文章
- HDU 6070 - Dirt Ratio | 2017 Multi-University Training Contest 4
比赛时会错题意+不知道怎么线段树维护分数- - 思路来自题解 /* HDU 6070 - Dirt Ratio [ 二分,线段树 ] | 2017 Multi-University Training ...
- hdu 6070 Dirt Ratio 线段树+二分
Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Spe ...
- 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 ...
- HDU 6070 Dirt Ratio(线段树)
Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Tot ...
- HDU 6070 Dirt Ratio(分数规划+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 找出一个区间,使得(区间内不同数的个数/区间长度)的值最小,并输出该值. 思路: 因为是要求$\f ...
- 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 ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- HDU 6070 二分+线段树
Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Tot ...
- 2017 多校4 Dirt Ratio
多校4 Dirt Ratio(二分+线段树) 题意: 给出n个数,找一段区间使得区间内不同数字个数除以区间长度最小,求这个最小值,\(n<=60000,a_i<=n\) 题解: 二分答案m ...
随机推荐
- 图论+思维(2019牛客国庆集训派对day2)
题意:https://ac.nowcoder.com/acm/contest/1107/J n个点的完全图编号0-n-1,第i个点的权值为2^i,原先是先手选取一些边,然后后手选取一些点,满足先手选取 ...
- 嵌套泛型参数IList<IList<Object>>如何传参
在调用第三方库的时候,有这么一个泛型参数,如下图: 按照经验,使用两个List嵌套声明变量即可: IList<IList<AnnotatedPoint2D>> outImag ...
- MySQL各大存储引擎
MySQL各大存储引擎: 最好先看下你下的MySQL支持什么数据库引擎 存储引擎主要有: 1. MyIsam , 2. InnoDB, 3. Memory, 4. Blackhole, 5. CSV, ...
- Linux的一个后门引发对PAM的探究
转自http://www.91ri.org/16803.html 1.1 起因 今天在搜索关于Linux下的后门姿势时,发现一条命令如下:软链接后门: 1 ln -sf /usr/sbin/ssh ...
- django 项目开发及部署遇到的坑
1.django 连接oracle数据库遇到的坑 需求:通过plsql建立的oracle数据表,想要django操作这几个表 python manage.py inspectdb table_name ...
- MySQL中的DML、DQL和子查询
一.MySQL中的DML语句 1.使用insert插入数据记录: INSERT INTO `myschool`.`student` (`studentNo`, `loginPwd`, `student ...
- java启动server时报端口无效解决方法
今天在Java里配置Tomcat服务器,启动时出现如下图报错信息 The server cannot be started because one or more of the ports are i ...
- java 获取视频时间
//先将视频保存到项目生成临时文件,获取时长后删除临时文件 // 使用fastdfs进行文件上传 @RequestMapping("/uploadVideoToFast") @Re ...
- 7、TortoiseSVN
7.TortoiseSVN TortoiseSVN图标介绍: 目录空白处右键→TortoiseSVN→Settings 7.1独立将工程上传到服务器的思路 12.2针对archetype-catalo ...
- Kubernetes介绍与核心组件
Kubernetes是什么? Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署.自动扩缩容.维护等功能. Kubernetes 特点 可移植: 支持公有云,私有云 ...