BZOJ 3524 [Poi2014]Couriers(二分+蒙特卡罗)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3524
【题目大意】
给一个长度为n的序列a。1≤a[i]≤n。
m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。
如果存在,输出这个数,否则输出0。
【题解】
我们可以在[l,r]中随机一个位置检验这个位置上数是不是答案,
检测方法可以在数组中保存每个数在序列中的不同位置,二分即可
选中答案的概率为1/2,我们做k次蒙特卡罗,正确率就为1-(1/2)^k,
复杂度为O(kmlogn)。
主席树做法见 LINK
【代码】
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N=500010;
vector<int> pos[N];
int a[N],l,r,n,m;
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++)pos[i].clear();
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pos[a[i]].push_back(i);
}
while(m--){
scanf("%d%d",&l,&r);
int len=r-l+1,find=0;
for(int k=1;k<=20;k++){
int u=a[l+rand()%len];
int st=lower_bound(pos[u].begin(),pos[u].end(),l)-pos[u].begin();
int en=upper_bound(pos[u].begin(),pos[u].end(),r)-pos[u].begin();
if((en-st)*2>len){find=u;break;}
}if(find)printf("%d\n",find);
else puts("0");
}
}return 0;
}
BZOJ 3524 [Poi2014]Couriers(二分+蒙特卡罗)的更多相关文章
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- BZOJ 3524 [Poi2014]Couriers(可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3524 [题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个 ...
- 【刷题】BZOJ 3524 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...
- 3524: [Poi2014]Couriers -- 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...
- 【BZOJ】3524: [Poi2014]Couriers
[算法]主席树 [题解]例题,记录和,数字出现超过一半就递归查找. 主席树见[算法]数据结构 #include<cstdio> #include<algorithm> #inc ...
- 【BZOJ】3524 [POI2014] Couriers(主席树)
题目 传送门:QWQ 传送到洛谷QWQ 分析 把求区间第k大的改一改就ok了. 代码 #include <bits/stdc++.h> using namespace std; ; ], ...
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
随机推荐
- LESS使用简介!
我真的真的极度痛苦. 原本用了那么久的LESS,一直都是用编译工具(考拉)进行编译的,今天试了试用less.js来搞,按官网的都一毛一样,然而!就是编译不出来! 我用来擦鼻涕的卫生纸都一下午用了大半卷 ...
- EditText中inputType详解
<EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- 数字签名算法rsa
数字签名算法消息传递模型 由消息发送方构建密钥对,这里由甲方完成. 由消息发送方公布公钥至消息接收方,这里由甲方将公钥公布给乙方. 注意如加密算法区别,这里甲方使用私钥对数据签名,数据与签名形成一则消 ...
- webpack版本1与版本2的若干写法区别
2.x的环境遇到类似this._init is not a function的报错. 版本1.x的写法: resolve: { extensions: ['', '.js', '.vue'] }, m ...
- Which cryptsetup
Which cryptsetup Rpm –qf ‘which cryptsetup’ 安装加密工具: 设置加密分区 Crptsetup luksFormat Echo –n “xuegod123” ...
- yocto 离线编译
使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...
- centos7安装libvirt支持xen
另外还有一个非常棒的用法 假如我要执行iostat这个命令来查看CPU与存储设备状态,可是执行却发现没有这个命令 于是执行yum install iostat,结果说找不到该软件,使用下面的办法可以解 ...
- tiny-rtems-src
https://github.com/RTEMS/rtems-libbsd https://github.com/freebsd/freebsd/tree/642b174daddbd0efd9bb5f ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- Permutations I&&II
Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...