HDU 6070 二分+线段树
Dirt Ratio
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 335 Accepted Submission(s): 105
Special Judge

Picture from MyICPC
Little Q is a coach, he is now staring at the submission list of a team. You can assume all the problems occurred in the list was solved by the team during the contest. Little Q calculated the team's low ''Dirt Ratio'', felt very angry. He wants to have a talk with them. To make the problem more serious, he wants to choose a continuous subsequence of the list, and then calculate the ''Dirt Ratio'' just based on that subsequence.
Please write a program to find such subsequence having the lowest ''Dirt Ratio''.
In each test case, there is an integer n(1≤n≤60000) in the first line, denoting the length of the submission list.
In the next line, there are n positive integers a1,a2,...,an(1≤ai≤n), denoting the problem ID of each submission.
For every problem, you can assume its final submission is accepted.
题意:给你一个长度为n的区间 X=区间不同数的个数 Y=区间长度 求子区间X/Y的最小值
题解:二分答案mid check X/Y<=mid
X/Y=X/(r-L+1)<=mid
=> X+L*mid<=(r+1)*mid
建树初始每个点的maxn为L*mid
从左向右遍历右边界 遍历到r 更新(last[r]+1,r) 值增加1 last[r]表示a[r]相同值的上一个位置
r之前的每个结点L存的是L到r不同数的个数X+L*mid (这些便是不等式的左边)取最小值 与 (r+1)*mid 比较
#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define LL long long
#define mod 1000000007
using namespace std;
int t;
int n;
int a[];
int last[];
map<int,int> mp;
struct node
{
int l,r;
double maxn;
int add;
} tree[];
void buildtree(int root ,int left,int right,double zz)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
if(left==right)
{
tree[root].maxn=left*zz;
return ;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid,zz);
buildtree(root<<|,mid+,right,zz);
tree[root].maxn=min(tree[root<<].maxn,tree[root<<|].maxn);
}
void pushdown(int root)
{
if(tree[root].add==) return ;
tree[root<<].add+=tree[root].add;
tree[root<<|].add+=tree[root].add;
tree[root<<].maxn+=tree[root].add;
tree[root<<|].maxn+=tree[root].add;
tree[root].add=;
}
void update(int root,int left,int right,int c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add+=c;
tree[root].maxn+=c;
return ;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
{
update(root<<,left,right,c);
}
else
{
if(left>mid)
update(root<<|,left,right,c);
else
{
update(root<<,left,mid,c);
update(root<<|,mid+,right,c); }
}
tree[root].maxn=min(tree[root<<].maxn,tree[root<<|].maxn);
}
double query(int root,int left,int right)
{
if(left>right)
return ;
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].maxn;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return min(query(root<<,left,mid),query(root<<|,mid+,right));
}
}
bool check (double x)
{
buildtree(,,n,x);
for(int i=; i<=n; i++)
{
update(,last[i]+,i,);
double zha=(double)query(,,i);
if(zha<=(i+)*x)
return true;
}
return false;
}
int main()
{
scanf("%d",&t);
for(int kk=;kk<=t;kk++){
scanf("%d",&n);
mp.clear();
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
last[i]=mp[a[i]];
mp[a[i]]=i;
}
double l=,r=1.0,mid,ans;
while(abs(l-r)>=0.00001)
{
mid=(l+r)/;
if(check(mid)){
ans=mid;
r=mid;
}
else
l=mid;
}
printf("%f\n",ans);
}
return ;
}
HDU 6070 二分+线段树的更多相关文章
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
随机推荐
- Python函数初识
一.函数是什么 计算机语言中的函数是类比于数学中的函数演变来的,但是又有所不同.前面的知识中我们学会了运用基础语法(列表.字典)和流程控制语句貌似也能处理一些复杂的问题,但是相对于相似的大量重复性 ...
- can总线实现stm32的IAP
使用stm32f105rct6的can通信做IAP,实现固件的远程更新功能.IAP的实现包括两个程序:BootLoader和应用程序.启动过程先启动BootLoader,等待1s,若接收到烧写指令则开 ...
- 10.openldap备份与恢复
备份方式 一.使用slapcat指令备份 使用slapcat备份后的数据 经过相关无用条目处理,即可实现数据上的条目备份 备份指令如下 #备份 #slapcat -v -l openldap-back ...
- [zabbix] zabbix数据采集频率、数据连续多次异常触发、告警次数、告警频率
数据采集频率:1分钟采集一次 数据连续多次异常触发:连续三次异常才触发告警 告警次数:告警三次 告警频率:每隔10分钟告警一次 默认模板“Template App Zabbix Agent”监控项“A ...
- JVM新生代老年代详解
1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...
- Task 1 四则运算
第一节课作业 1.设计思想:随机产生30道题目,主体是一个for循环,在循环体中加上相应的内容,包括随机数函数得到两个运算数值以及运算符号,再利用switch,case结构输出相应的题目即可. 2.源 ...
- BATA冲刺准备
目录 第一部分 调研,评测 福大助手的bug IOS端 Android端 福大助手结构体系的思维导图 为什么开发人员没有发现这个bug 假设团队开发这款app,应注意哪些方面(架构.部署运维.微服务等 ...
- 在数组中找出两数之和为10的所有组合(JAVA)
/*利用冒泡排序实现*/ import java.util.Scanner;public class Paixun { public static void main(String[] args) { ...
- 基于GUI的小学生四则运算系统
前言:首先在此感谢我的结对搭档,没有她的帮助和引导绝不会有现在的项目.很高兴和她一起结对完成这个项目.搭档真的是棒棒哒! 一.Coding.Net项目地址: https://git.coding.ne ...
- struts2与springmvc的区别
1 机制不同 springmvc的入口是servlet,struts2的入口是filter,导致了二者的机制不同: 2 性能不同 spring会稍微比struts快.spring mvc是基于方法的设 ...