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中的最小值

  (思路来源 标程之类的东西)

  

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int M=6e4+55;
  10.  
  11. int n;
  12. int s[M],lst[M];
  13. double tree[M*3],tag[M*3];
  14.  
  15. void build(int rt,int li,int ri,double val)
  16. {
  17. tag[rt]=0;
  18. if(li==ri)
  19. {
  20. tree[rt]=val*li;
  21. return ;
  22. }
  23. int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
  24. build(lc,li,mid,val);
  25. build(rc,mid+1,ri,val);
  26. tree[rt]=min(tree[lc],tree[rc]);
  27. }
  28.  
  29. void pushdown(int rt,int li,int ri)
  30. {
  31. if(li==ri)
  32. {
  33. tag[rt]=0;
  34. return ;
  35. }
  36. int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
  37. tree[lc]+=tag[rt];
  38. tree[rc]+=tag[rt];
  39. tag[lc]+=tag[rt];
  40. tag[rc]+=tag[rt];
  41. tag[rt]=0;
  42. }
  43.  
  44. void update(int rt,int li,int ri,int lq,int rq,double val) //add
  45. {
  46. if(lq<=li && ri<=rq)
  47. {
  48. tag[rt]+=val;
  49. tree[rt]+=val;
  50. return ;
  51. }
  52. int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
  53. if(tag[rt])
  54. pushdown(rt,li,ri);
  55. if(mid>=lq)
  56. update(lc,li,mid,lq,rq,val);
  57. if(mid+1<=rq)
  58. update(rc,mid+1,ri,lq,rq,val);
  59. tree[rt]=min(tree[lc],tree[rc]);
  60. }
  61.  
  62. double query(int rt,int li,int ri,int lq,int rq) //get min
  63. {
  64. double ret=1e9+7;
  65. if(lq<=li && ri<=rq)
  66. return tree[rt];
  67. int mid=(li+ri)>>1,lc=(rt<<1),rc=(rt<<1)+1;
  68. if(tag[rt])
  69. pushdown(rt,li,ri);
  70. if(mid>=lq)
  71. ret=min(ret,query(lc,li,mid,lq,rq));
  72. if(mid+1<=rq)
  73. ret=min(ret,query(rc,mid+1,ri,lq,rq));
  74. return ret;
  75. }
  76.  
  77. bool check(double val)
  78. {
  79. int i,j;
  80. double tmp;
  81. build(1,1,n,val);
  82. memset(lst,0,sizeof(lst));
  83. for(i=1;i<=n;i++)
  84. {
  85. update(1,1,n,lst[s[i]]+1,i,1);
  86. tmp=query(1,1,n,1,i); //tmp=distinct(l,r)+val*l
  87. if(tmp<val*(i+1)) //distinct(l,r)+val*l<val*(r+1) => distinct(l,r)/(r-l+1)<val
  88. return true;
  89. lst[s[i]]=i;
  90. }
  91. return false;
  92. }
  93.  
  94. void solve()
  95. {
  96. double li=0,ri=1,mid;
  97. int i,j,cnt=20;
  98. while(cnt--)
  99. {
  100. mid=(li+ri)/2;
  101. if(check(mid))
  102. ri=mid;
  103. else
  104. li=mid;
  105. }
  106. printf("%.10lf\n",ri);
  107. }
  108.  
  109. int main()
  110. {
  111. int i,j;
  112. int cas;
  113. cin>>cas;
  114. while(cas--)
  115. {
  116. scanf("%d",&n);
  117. for(i=1;i<=n;i++)
  118. scanf("%d",&s[i]);
  119. solve();
  120. }
  121. return 0;
  122. }

  

hdu 6070 Dirt Ratio的更多相关文章

  1. HDU 6070 - Dirt Ratio | 2017 Multi-University Training Contest 4

    比赛时会错题意+不知道怎么线段树维护分数- - 思路来自题解 /* HDU 6070 - Dirt Ratio [ 二分,线段树 ] | 2017 Multi-University Training ...

  2. hdu 6070 Dirt Ratio 线段树+二分

    Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Spe ...

  3. 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 ...

  4. HDU 6070 Dirt Ratio(线段树)

    Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Tot ...

  5. HDU 6070 Dirt Ratio(分数规划+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 找出一个区间,使得(区间内不同数的个数/区间长度)的值最小,并输出该值. 思路: 因为是要求$\f ...

  6. 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 ...

  7. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  8. HDU 6070 二分+线段树

    Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Tot ...

  9. 2017 多校4 Dirt Ratio

    多校4 Dirt Ratio(二分+线段树) 题意: 给出n个数,找一段区间使得区间内不同数字个数除以区间长度最小,求这个最小值,\(n<=60000,a_i<=n\) 题解: 二分答案m ...

随机推荐

  1. 图论+思维(2019牛客国庆集训派对day2)

    题意:https://ac.nowcoder.com/acm/contest/1107/J n个点的完全图编号0-n-1,第i个点的权值为2^i,原先是先手选取一些边,然后后手选取一些点,满足先手选取 ...

  2. 嵌套泛型参数IList<IList<Object>>如何传参

    在调用第三方库的时候,有这么一个泛型参数,如下图: 按照经验,使用两个List嵌套声明变量即可: IList<IList<AnnotatedPoint2D>>  outImag ...

  3. MySQL各大存储引擎

    MySQL各大存储引擎: 最好先看下你下的MySQL支持什么数据库引擎 存储引擎主要有: 1. MyIsam , 2. InnoDB, 3. Memory, 4. Blackhole, 5. CSV, ...

  4. Linux的一个后门引发对PAM的探究

    转自http://www.91ri.org/16803.html 1.1   起因 今天在搜索关于Linux下的后门姿势时,发现一条命令如下:软链接后门: 1 ln -sf /usr/sbin/ssh ...

  5. django 项目开发及部署遇到的坑

    1.django 连接oracle数据库遇到的坑 需求:通过plsql建立的oracle数据表,想要django操作这几个表 python manage.py inspectdb table_name ...

  6. MySQL中的DML、DQL和子查询

    一.MySQL中的DML语句 1.使用insert插入数据记录: INSERT INTO `myschool`.`student` (`studentNo`, `loginPwd`, `student ...

  7. java启动server时报端口无效解决方法

    今天在Java里配置Tomcat服务器,启动时出现如下图报错信息 The server cannot be started because one or more of the ports are i ...

  8. java 获取视频时间

    //先将视频保存到项目生成临时文件,获取时长后删除临时文件 // 使用fastdfs进行文件上传 @RequestMapping("/uploadVideoToFast") @Re ...

  9. 7、TortoiseSVN

    7.TortoiseSVN TortoiseSVN图标介绍: 目录空白处右键→TortoiseSVN→Settings 7.1独立将工程上传到服务器的思路 12.2针对archetype-catalo ...

  10. Kubernetes介绍与核心组件

    Kubernetes是什么? Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署.自动扩缩容.维护等功能. Kubernetes 特点 可移植: 支持公有云,私有云 ...