题目描述

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);
}
}

最新文章

  1. TC SRM 593 DIV1 250
  2. Eclipse修改Tomcat发布路径以及的配置多个Tomcat方法
  3. 该如何认识ZBrush中的2.5D绘画
  4. BZOJ4299 : Codechef FRBSUM
  5. Android 进阶 Fragment 介绍和使用 (二)
  6. NSKeyValueObserving(KVO)
  7. IMAP与POP3的区别
  8. 反编译C#的dll文件并修改,再重新生成dll
  9. iOS之上架打包时报错:ERROR ITMS-90086: &quot;Missing 64-bit support.
  10. 首页商品图片显示错位,easy-popular批量上传
  11. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
  12. 连接虚拟机mysql无法访问,报错编号1130的解决方法
  13. Docker学习笔记 - Docker容器的日志
  14. 改变选择文字的color及background-color
  15. Python学习之旅(三十)
  16. (4.8)mysql备份还原——binlog查看工具之show binlog的使用
  17. A1020. Tree Traversals
  18. jquery 之ajax,get,post异步请求简单代码模版
  19. out参数ref参数params 可变参数
  20. Linux内核分析 - 网络[十四]:IP选项

热门文章

  1. sorted内置函数
  2. Python 获取文件类型后缀
  3. svn客户端清空账号信息的两种方法
  4. mybatis中封装结果集常见示例
  5. VueJS中学习使用Vuex详解
  6. 利用Matlab实现PID控制仿真
  7. 深度探索MySQL主从复制原理
  8. Android--TextView第一个单词大写
  9. CharacterEncodingFilter cannot be cast to javax.servlet.Filter
  10. OAuth2实现原理