hdu6070
hdu6070
题意
给出 \(n\) 个数, \(\frac{x}{y}\) 表示某个区间不同数的个数除以区间的长度,求 \(\frac{x}{y}\) 最小值。
分析
设 \(size(l, r)\) 表示 \([l, r]\) 这个区间内不同数的个数,那么求得就是 \(\frac{size(l, r)}{r - l + 1}\) 的最小值。
二分答案 \(mid\) ,式子转化成 \(size(l, r) + mid \times l \leq mid \times (r + 1)\) 。
枚举右端点 \(r\),线段树存的是从 \(l\) 到 当前枚举到的 \(r\) 的 \(size(l, r) + mid \times l\) ,如果存在最小值满足小于等于 \(mid \times (r + 1)\) ,说明这个 \(mid\) 可以取到,更新 \(mid\) 。
新姿势:线段树内每个点的信息是动态的,和枚举到的 \(r\) 有关,通过区间更新就可以很方便的计算 \(size(l, r)\) 的值。
\(last[a[i]]\) 表示 \(a[i]\) 这个数上一次出现的位置,那么每次我们只需要区间更新 \([last[a[i]] + 1, i]\) 就好了。
code
#include<bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
const int MAXN = 6e4 + 10;
int a[MAXN];
int last[MAXN];
double s[MAXN << 2];
double lazy[MAXN << 2];
void pushDown(int rt) {
if(lazy[rt] > 0) {
s[rt << 1] += lazy[rt];
s[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void pushUp(int rt) {
s[rt] = min(s[rt << 1], s[rt << 1 | 1]);
}
void update(int L, int R, double c, int l, int r, int rt) {
if(L <= l && r <= R) {
lazy[rt] += c;
s[rt] += c;
return;
}
int m = (l + r) / 2;
pushDown(rt);
if(L <= m) update(L, R, c, lson);
if(R > m) update(L, R, c, rson);
pushUp(rt);
}
double query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return s[rt];
}
int m = (l + r) / 2;
pushDown(rt);
double res = 1e12;
if(L <= m) res = query(L, R, lson);
if(R > m) res = min(res, query(L, R, rson));
pushUp(rt);
return res;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
double l = 0, r = 1.0, mid;
while(r - l > 1e-5) {
mid = (l + r) / 2.0;
memset(s, 0, sizeof s);
memset(lazy, 0, sizeof lazy);
memset(last, 0, sizeof last);
int flg = 0;
for(int i = 1; i <= n; i++) {
update(last[a[i]] + 1, i, 1, 1, n, 1);
update(i, i, mid * i, 1, n, 1);
if(query(1, i, 1, n, 1) <= mid * (i + 1)) {
flg = 1;
break;
}
last[a[i]] = i;
}
if(flg) r = mid;
else l = mid;
}
printf("%.6f\n", mid);
}
return 0;
}
hdu6070的更多相关文章
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- 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
size(l,r)表示区间l,r权值的种类数,让你求min{size(l,r)/(r-l+1)}(1<=l<=r<=n). last[r]表示a[r]上一次出现的位置, 就是二分验证 ...
- hdu6070(分数规划/二分+线段树区间更新,区间最值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...
- HDU-6070 Dirt Ratio(二分+线段树+分数规划)
目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门 原题目描述在最下面. 求\(sum/len\)最小值.\(sum\)是一段区间内不同数字的 ...
随机推荐
- [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
[CERC2017]Intrinsic Interval https://www.luogu.org/blog/ywycasm/solution-p4747# 这种“好的区间”,见得还是比较多的了. ...
- codeforces803D. Magazine Ad
D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...
- hdu Shell Necklace 5730 分治FFT
Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...
- Python之日志操作(logging)
import logging 1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...
- supervisor提高nodejs调试效率
1.NodeJS环境安装 2.安装supervisor npm install supervisor -g (表示安装到全局路径下) 开发nodejs程序,调试的时候,无论你修改了代码的哪一部分,都 ...
- 记录一次Nginx跳转报错的问题
错误信息如下: An error occurred. Sorry, the page you are looking for is currently unavailable. Please try ...
- sql注入预防
在我们登陆验证时会发现sql注入的现象. 1.sql注入发生原因 因为如果用户在用户名上输入的是' or 1=1 # 时,我们得到的sql语句是select * from shop_user wher ...
- DotNet 学习笔记 MVC模型
Model Binding Below is a list of model binding attributes: •[BindRequired]: This attribute adds a mo ...
- [bzoj2763][JLOI2011]飞行路线——分层图最短路
水题.不多说什么. #include <bits/stdc++.h> using namespace std; const int maxn = 10010; const int maxk ...
- 逐步实现python版wc命令
Python 如何处理管道输入输出 sys.stdin 等于打开了一个文件对象,所有输入的文件都会写入到标准输入文件中(键盘) sys.stdout 等于打来了一个文件对象,使用.write()把信息 ...