链接:https://ac.nowcoder.com/acm/contest/881/A
来源:牛客网

题目描述

Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r)=RMQ(v,l,r)RMQ(u,l,r)=RMQ(v,l,r) for all 1≤l≤r≤m1≤l≤r≤m
where RMQ(w,l,r)RMQ(w,l,r) denotes the index of the minimum element among wl,wl+1,…,wrwl,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≤np≤n where {a1,a2,…,ap}{a1,a2,…,ap} and {b1,b2,…,bp}{b1,b2,…,bp} are equivalent.

input

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 a1,a2,…,ana1,a2,…,an.
The third line contains n integers b1,b2,…,bnb1,b2,…,bn. * 1≤n≤1051≤n≤105
* 1≤ai,bi≤n1≤ai,bi≤n
* {a1,a2,…,an}{a1,a2,…,an} are distinct.
* {b1,b2,…,bn}{b1,b2,…,bn} are distinct.
* The sum of n does not exceed 5×1055×105. OUTPUT
For each test case, print an integer which denotes the result.
输入

输出


题意:给出两个序列A和B,让你找出最大的一个p,使得在区间[1,p]内,任意的l,r∈[1,n],RMQ(l,r,A)要等于RMQ(l,r,B)。RMQ(l,r,A)返回的是[l,r]内最小值对应的下标。
思路:让ai和bi同时进入单调队列,单调队列保存元素的值和下标,如果进队/出队的时候,ai所在的单调队列和bi所在的单调队列的下标值是一样的,那么就是OK的,直到找到状态不一样的位置,那么答案为它的前一个。渐进时间复杂度O(n)。
AC代码:
 #include<bits/stdc++.h>

 using namespace std;
struct str{
int val;
int pos; };
int main(){
int n;
while(~scanf("%d",&n)){
int a[n+];
int b[n+];
deque<str> q1,q2;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
q1.push_front((str){-,});
q2.push_front((str){-,});
int arr1[n+];
int arr2[n+];
for(int i=;i<=n;i++){
while(!q1.empty()&&q1.front().val>a[i])
q1.pop_front();
arr1[i]=q1.front().pos;
q1.push_front((str){a[i],i});
while(!q2.empty()&&q2.front().val>b[i])
q2.pop_front();
arr2[i]=q2.front().pos;
q2.push_front((str){b[i],i});
}
int ans=; for(int i=;i<=n;i++){
if(arr1[i]==arr2[i]){
ans=i;
}else{
break;
}
}
printf("%d\n",ans);
} return ;
}

2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)的更多相关文章

  1. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  2. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  5. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

  6. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  7. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  8. 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)

    题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...

  9. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

随机推荐

  1. IDEA插件之FindBugs

    1.是个啥? Findbugs,它是一个静态分析工具,用来查找Java代码中的程序错误.它使用静态分析来识别Java程序中上百种不同类型的潜在错误. 2.安装 File -> Settings ...

  2. 【LOJ】#3096. 「SNOI2019」数论

    LOJ#3096. 「SNOI2019」数论 如果\(P > Q\)我们把\(P\)和\(Q\)换一下,现在默认\(P < Q\) 这个时候每个合法的\(a_i\)都可以直接落到\(Q\) ...

  3. 虚拟机(VM)安装openwrt-koolshare软路由

    ⒈创建虚拟机 **软路由选择Windows操作系统,因为我们需要在PE环境中进行软路由的写入,固件类型选择BIOS,网络类型选择使用仅主机模式网络,虚拟磁盘类型选择IDE[一定要选择IDE模式],SC ...

  4. Nginx部署前后端分离的单页应用配置

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  5. C++继承种类

  6. CentOS7 PHP cURL errno 35, 原因:CentOS7中没有安装curl和OpenSSL的最新版

    安装OpenSSL的最新版 话不多说,直接上安装步骤 #cd /usr/local/src # 跳过证书获取失败 # wget https://www.openssl.org/source/opens ...

  7. 通过ImageReader进行图像裁剪时出现NoSuchElementException异常

    首先放上最初的Image工具类 package util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import ...

  8. 第十二章 ZYNQ-MIZ702 PS读写PL端BRAM

      本篇文章目的是使用Block Memory进行PS和PL的数据交互或者数据共享,通过zynq PS端的Master GP0端口向BRAM写数据,然后再通过PS端的Mater GP1把数据读出来,将 ...

  9. varnish应用

    Nginx+Varnish+基本业务 ngnix nginx.conf配置文件 user root; worker_processes ; error_log logs/error.log crit; ...

  10. 怎样设置cookie的到期时间

    1. 使用Cookie的: Expires 属性. 它可以设置cookie的过期时间. 下面的代码表示id这条cookie的过期时间是2015年10月21日早上7点28分; Set-Cookie: i ...