题目描述

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) for all 1≤l≤r≤m1≤l≤r≤m

where RMQ(w,l,r) denotes the index of the minimum element among 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 where{a1,a2,…,ap} and {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 \(a1,a2,…,an\)

The third line contains \(n\) integers \(b1,b2,…,bn\)

  • \(1≤n≤10^5\)
  • \(1≤ai,bi≤n\)
  • \(\{a1,a2,…,an\}\) are distinct.
  • \(\{b1,b2,…,bn\}\) are distinct.
  • The sum of n does not exceed \(5×10^5\).

输出描述:

For each test case, print an integer which denotes the result.

题解

考虑一个数\(a_i\)会对哪些区间的\(RMQ\)造成影响,显然是向左直到第一个比它小的数的位置为止的区间都有影响,那么就有一种判定方法,顺次枚举\(i\),判断\(a_i\)和\(b_i\)向左能影响到的区间,如果能影响到的区间不是完全相同的那么肯定有一个\(RMQ\)不同。

所以现在的问题就变成了维护一个数据结构,可以求出前缀\(max\)。显然单调栈,单调队列,BIT都可以维护(BIT的话以数值为BIT的下标,原数组下标为储存的值,问题就变成了前缀\(max\))

用BIT的话是\(O(n\log n)\)的。单调队列和单调栈的话是\(O(n)\)的。

标算是单调队列,还提供了另一个做法:

题中的“equivalent”等价于笛卡尔树相同,

二分答案,比较两个前缀的笛卡尔树 \(O(n \log n)\)

题解中对单调队列的做法的证明也用了笛卡尔树。

#include <bits/stdc++.h>
using namespace std; const int N = 100010;
int a[N], b[N];
int n;
/*
需要一个数据结构,往前找找到第一个数比它小的,返回位置
*/
namespace BIT {
int c[N][2];
#define lowbit(i) (i & -i)
void add(int x, int v, int id) {
for(int i = x; i <= n; ++i) c[i][id] = max(c[i][id], v);
}
int query(int x, int id) {
int ans = 0;
for(int i = x; i; i -= lowbit(i)) ans = max(ans, c[i][id]);
return ans;
}
} using namespace BIT; int main() {
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; ++i) c[i][0] = c[i][1] = 0;
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= n; ++i) scanf("%d", &b[i]);
int i;
for(i = 1; i <= n; ++i) {
int l = query(a[i], 0), r = query(b[i], 1);
if(l != r) break;
add(a[i], i, 0); add(b[i], i, 1);
}
printf("%d\n", --i);
}
}

2019牛客多校第一场 A.Equivalent Prefixes的更多相关文章

  1. 2019 牛客多校第一场 A Equivalent Prefixes

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题目大意 定义 RMQ(u, L, R) 为 u 数组在区间 [L, R] 上最小值的下标. 如果有 2 ...

  2. 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)

    Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...

  3. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  4. 2019牛客多校第一场E ABBA(DP)题解

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

  5. 2019牛客多校第一场A-Equivalent Prefixes

    Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...

  6. 2019 牛客多校第一场 D Parity of Tuples

    题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...

  7. 2019牛客多校第一场 E-ABBA(dp)

    ABBA 题目传送门 解题思路 用dp[i][j]来表示前i+j个字符中,有i个A和j个B的合法情况个数.我们可以让前n个A作为AB的A,因为如果我们用后面的A作为AB的A,我们一定也可以让前面的A对 ...

  8. 【2019牛客多校第一场】XOR

    题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...

  9. 2019 牛客多校第一场 B Integration

    题目链接:https://ac.nowcoder.com/acm/contest/881/B 题目大意 给定 n 个不同的正整数 ai,求$\frac{1}{\pi}\int_{0}^{\infty} ...

随机推荐

  1. CentOS安装部署jumperserver(堡垒机)

    可以参考官方的文档:http://docs.jumpserver.org/zh/docs/introduce.html 测试环境 系统: CentOS 7 IP: 192.168.244.144 设置 ...

  2. windows server系统打印服务配置

    系统环境:windows server 2008 R2 Enterprise Service Pack 1 安装内存:8G 系统类型:64位操作系统 目标:在此系统上开启打印服务,可以添加网络打印机 ...

  3. python基础 — Mysql Server

    sql server对于字符类型的有:char:固定长度,存储ANSI字符,不足的补英文半角空格.nchar:固定长度,存储Unicode字符,不足的补英文半角空格varchar:可变长度,存储ANS ...

  4. 「LibreOJ NOI Round #2」不等关系

    「LibreOJ NOI Round #2」不等关系 解题思路 令 \(F(k)\) 为恰好有 \(k\) 个大于号不满足的答案,\(G(k)\) 表示钦点了 \(k\) 个大于号不满足,剩下随便填的 ...

  5. Unity的学习笔记(鼠标移动控制视角移动)

    using UnityEngine; public class MouseLook : MonoBehaviour { , MouseX = , MouseY = } //定义一个枚举,移动xy,或者 ...

  6. Unity项目 - 坦克大战3D TankBattle

    目录 游戏原型 项目演示 绘图资源 代码实现 技术探讨 参考来源 游戏原型 游戏玩法:在有界的战场上,玩家将驾驶坦克,代表绿色阵营,与你的队友一起击溃红蓝阵营的敌人,在这场三方大战中夺得胜利! 操作指 ...

  7. C#使用Linq to XML进行XPath查询

    最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...

  8. SQL Server 2012使用日常

    SQL Server 2012个人使用日常(持续完善中) 1.查询筛选 2.修改数据

  9. webpack+vue搭建vue项目

    阅读地址: https://www.jianshu.com/p/23beadfa4aa5 源码地址:https://github.com/Ezoio/IMI-SOURCE-CODE

  10. Python接口自动化基础---get请求

    1.没有参数的get请求 import requests r=requests.get('http://docs.python-requests.org/zh_CN/latest/user/quick ...