2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A
来源:牛客网
Equivalent Prefixes
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
Two arrays u and v each with m distinct elements are called equivalent if and only if
R
M
Q
(
u
,
l
,
r
)
R
M
Q
(
v
,
l
,
r
)
RMQ(u,l,r)=RMQ(v,l,r) for all
1
≤
l
≤
r
≤
m
1≤l≤r≤m
where
R
M
Q
(
w
,
l
,
r
)
RMQ(w,l,r) denotes the index of the minimum element among
w
l
,
w
l
+
1
,
…
,
w
r
wl,wl+1,…,wr.
Since the array contains distinct elements, the definition of minimum is unambiguous.
Bobo has two arrays a and b each with n distinct elements. Find the maximum number
p
≤
n
p≤n where
{
a
1
,
a
2
,
…
,
a
p
}
{a1,a2,…,ap} and
{
b
1
,
b
2
,
…
,
b
p
}
{b1,b2,…,bp} are equivalent.
输入描述:
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains an integer n.
The second line contains n integers
a
1
,
a
2
,
…
,
a
n
a1,a2,…,an.
The third line contains n integers
b
1
,
b
2
,
…
,
b
n
b1,b2,…,bn.
1
≤
n
≤
10
5
1≤n≤105
*
1
≤
a
i
,
b
i
≤
n
1≤ai,bi≤n
*
{
a
1
,
a
2
,
…
,
a
n
}
{a1,a2,…,an} are distinct.
*
{
b
1
,
b
2
,
…
,
b
n
}
{b1,b2,…,bn} are distinct.
- The sum of n does not exceed
5
×
10
5
5×105.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
复制
2
1 2
2 1
3
2 1 3
3 1 2
5
3 1 5 2 4
5 2 4 3 1
输出
复制
1
3
4
题意:
给你两个数组a,b,大小为n,让你寻找一个数p (1<= p <= n) ,使之在 1~p 任意一个区间中a,b数组的最小值下标相同。
思路:
容易知道p的取值具有单调性,首先我们用st表在对数组a,b进行预处理,方便后续的RMQ,因为数组中的数相互不同,那么我们就可以直接RMQ获得区间最小值下标。
在二分p的过程中,我们这样来判断mid是否合法:
询问1~mid 区间两个数组中的最小值下标是否一致,如果不一致直接返回false,否则 以最小值下标minid为分界点递归处理1minid,minid+1mid。这个过程是O(n)的
所以总体时间复杂度是 O( n* log n )
细节见代码:
#include<iostream>
using namespace std;
const int maxn=2e5+10;
int n;
int a[maxn],b[maxn];
int sa[maxn][20],sb[maxn][20],mn[maxn];
void init()
{
mn[0]=-1;
for (int i=1;i<=n;i++)
{
mn[i]=((i & (i-1))==0) ? mn[i-1]+1 : mn[i-1];
sa[i][0]=a[i];
sb[i][0]=b[i];
}
for (int j=1;j<=mn[n];j++)
for (int i=1;i+(1<<j)-1<=n;i++)
{
sa[i][j]=min(sa[i][j-1],sa[i+(1<<(j-1))][j-1]);
sb[i][j]=min(sb[i][j-1],sb[i+(1<<(j-1))][j-1]);
}
}
int ida[maxn];
int idb[maxn];
int rqa(int L,int R)
{
int k=mn[R-L+1];
// cout<<"a "<<min(sa[L][k],sa[R-(1<<k)+1][k])<<endl;
return ida[min(sa[L][k],sa[R-(1<<k)+1][k])];
}
int rqb(int L,int R)
{
int k=mn[R-L+1];
// cout<<"b "<<min(sb[L][k],sb[R-(1<<k)+1][k])<<endl;
return idb[min(sb[L][k],sb[R-(1<<k)+1][k])];
}
bool pan(int l,int r)
{
if(l>=r)
{
return 1;
}
int w=rqb(l,r);
int q=rqa(l,r);
if(w!=q)
{
return 0;
}else{
return pan(l,w-1)&&(pan(q+1,r));
}
}
bool check(int mid)
{
// bool res=1;
return pan(1,mid);
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ida[a[i]]=i;
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
idb[b[i]]=i;;
}
init();
int l=1;
int r=n;
int mid;
int ans=1;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))
{
l=mid+1;
ans=mid;
}else{
r=mid-1;
}
}
printf("%d\n",ans);
}
}
2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)的更多相关文章
- 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)
题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...
- 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem
题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3 4 2 3 4 输出:0 0 1 题解: 认真想一 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第八场)E.Explorer
链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...
随机推荐
- IDEA全局配置
进入全局设置界面: 取消每次启动IDEA就默认打开上一次最后关闭的项目 编译器代码字体设置: 控制台字体大小和颜色设置 同一个文件代码里面的各个不同方法之间显示分割线 代码自动提示不区分大小写 格式化 ...
- Java反序列化漏洞从入门到深入(转载)
前言 学习本系列文章需要的Java基础: 了解Java基础语法及结构(菜鸟教程) 了解Java面向对象编程思想(快速理解请上知乎读故事,深入钻研建议买本<疯狂Java讲义>另外有一个刘意老 ...
- Python_序列对象内置方法详解_String
目录 目录 前言 软件环境 序列类型 序列的操作方法 索引调用 切片运算符 扩展切片运算符 序列元素的反转 连接操作符 重复运算符 成员关系符 序列内置方法 len 获取序列对象的长度 zip 混合两 ...
- elasticsearch head + xpack 用户名密码访问
修改配置文件elasticsearch.yml,增加http.cors.allow-headers: Authorization 访问head时,url如下所示:http://192.168.100. ...
- IK分词器的安装与使用IK分词器创建索引
之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了. 1. i ...
- C++编译错误提示 [Error] name lookup of 'i' changed for ISO '
在VC 6 中,i的作用域范围是函数作用域,在for循环外仍能使用变量i 即: for (int i = 0; i < n; ++i) { //…… } cout<< ...
- 【BW系列】SAP BW on HANA 迁移问题
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BW系列]SAP BW on HANA 迁移问 ...
- 【HANA系列】SAP 【第二篇】EXCEL连接SAP HANA的方法(ODBC)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP [第二篇]EXCEL连接 ...
- Cocos2d-X多线程(1) 在cocos2d-x中使用多线程
教科书上说:进程是资源分配的最小单位,线程是CPU调度的最小单位. 进程是程序在计算机上的一次执行活动.直观的讲就是会产生一个pid. int main() { //业务逻辑代码 re ...
- java使用face++简单实现人脸识别注册登录
java使用face++简单实现人脸识别注册登录 前言 人脸识别,好高大上!!! 理解之后很简单. 支付宝使用的就是face++, 至于face++账号信息,apikey…..,本文不做讲述,网上很多 ...