CodeForces 1187D Subarray Sorting
Problem
You are given an array \(a_1\),\(a_2\),…,\(a_n\) and an array \(b_1\),\(b_2\),…,\(b_n\).
For one operation you can sort in non-decreasing order any subarray \(a[l…r]\) of the array \(a\).
For example, if \(a=[4,2,2,1,3,1]\) and you choose subbarray \(a[2…5]\), then the array turns into \([4,1,2,2,3,1]\).
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array \(a\).
Input
The first line contains one integer \(t (1≤t≤3⋅10^5)\) — the number of queries.
The first line of each query contains one integer \(n (1≤n≤3⋅105)\).
The second line of each query contains \(n\) integers \(a_1,a_2,…,a_n (1≤a_i≤n)\).
The third line of each query contains n integers \(b_1,b_2,…,b_n (1≤b_i≤n)\).
It is guaranteed that \(\sum{n}≤3⋅10^5\) over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray \(a_1…a_5\), then a will turn into \([1,1,4,4,7,5,6]\), and then sort subarray \(a_5…a_6\).
感想
这题也过分,一定要发朋友圈博客。
OI带到这里的“坏”习惯——及时break。ACM里遍地都是多组数据,判断出一组答案,及时break之后,这一组还没读完读数据就被下一组读了,自然WA了。
这题调了一上午的原因就在于此——经验不足,以至于完全看不出我的代码及时break的问题。
另外,这是我的第一篇用markdown写的博客,markdown真好用。
解题思路
支持的操作是对子区间升序排序,这意味着,我们可以把一个区间内的最大值移动到该区间最右端,也可以把一个区间内的最小值移动到该区间最左端。
由于b是从左到右输入,对于每一个b,a也要将合适的数字左移,所以我们移动区间最小值到最左端。其实想一想不一定总是要到最左端,一个区间内的最小值可以左移到任意位置,还不会影响其他数字的顺序(每次排序最小值和最小值左边的第一个数,总共两个数)。
首先读入a数组,经过一堆后面会说的预处理(为了快速找到下文说的pos[b]),然后依次读入b。
对于每一个读入的b(假设是第i个),寻找这个数字b在a中最早出现的位置(pos[b])。
- 如果找到的位置为无穷,换句话说a里没有这个数,那么答案就是NO,就可以break了就可以把flag标记设置成false,然后读完剩下数据,输出NO。
- 如果找到了一个位置,那么我们的目标就是把这个b向前移动到位置i,由之前的思考可得,b需要是是a数组里下标i到pos[b]之间的最小值。
- 再进一步,能读入第i个b,说明前面i-1个位置已经匹配完成了,或前面或后面,已经有i-1个数被设置成了INF,一定大于b,不会影响我们。
- 考虑1到pos[b]这个区间,假设里面的情况这样 █ █ █ █ ▂ █ █b, █ 形象地代表大于等于b的数字,它们不会干扰我们左移b。 ▂ 形象地代表小于b的数字,当然还没被使用(使用了就被设置成INF了),它会阻挡b左移,换句话说,b只能向左移动到 ▂ 的右边那段(或者不移动)。
- 如果现在i在 ▂ 左边,或者说i到pos[b]的最小值小于b,那答案也就是NO了。
- 如果现在i在 ▂ 右边,或者说i到pos[b]的最小值就是b,那么这次移动就可以完成。可是,完成这次以后,我们会发现, ▂ 还是在左边,没有被使用。由于a和b是同样长的,a、b含有不同数字的情况我们之前也排除了,所以在以后的时光里(雾), ▂ 一定会被用到, ▂ 就需要被右移(把 ▂ 右边的左移),但是, ▂ 的右边至少有个b挡着啊,b比 ▂ 大, ▂ 不可能右移到目标位置的。
- 综上,1到pos[b]的区间内,如果最小值小于b,那答案无论如何都是NO。
- 类似的,我们也可以证明(其实是太晚了,想睡觉了),如果1到pos[b]内的最小值总是等于b,那么目标可以达成,答案是YES
然后完善一些小地方,区间最小值连上更新,就用线段树,挺方便。快速找pos[b],思路类似莫队里经常出现的pos数组和nxt数组(SDOI2009 HH的项链)。另外,memset不要每组数组都全部清零,第18个测试点很变态(见代码59行)。
源代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
int T;
int n;
int a[300010], nxt[300010], pos[300010];
struct Segtree
{
int l, r;
int min;
} s[1200010];
inline int lson(int x) { return x << 1; }
inline int rson(int x) { return x << 1 | 1; }
inline void pushup(int x) { s[x].min = std::min(s[lson(x)].min, s[rson(x)].min); }
void build(int x, int l, int r)
{
s[x] = {l, r, 0};
if (l == r)
{
s[x].min = a[l];
return;
}
int mid = l + r >> 1;
build(lson(x), l, mid);
build(rson(x), mid + 1, r);
pushup(x);
}
void update(int x, int position)
{
if (position > s[x].r || position < s[x].l)
return;
if (s[x].l == s[x].r && s[x].l == position)
{
s[x].min = 0x7fffffff;
return;
}
update(lson(x), position);
update(rson(x), position);
pushup(x);
}
int que(int x, int l, int r)
{
if (r < s[x].l || l > s[x].r)
return 0x7fffffff;
if (l <= s[x].l && s[x].r <= r)
return s[x].min;
return std::min(que(lson(x), l, r), que(rson(x), l, r));
}
int main()
{
//freopen("test.in","r",stdin);
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", a + i);
memset(nxt, 0x7f, sizeof(int)*(n+2));//第18个测试点十分丧病,T是几十万,每组n是1
memset(pos, 0x7f, sizeof(int)*(n+2));
for (int i = n; i >= 1; i--)
{
nxt[i] = pos[a[i]];
pos[a[i]] = i;
}
build(1, 1, n);
bool ok = 1;
for (int i = 1, b; i <= n; i++)
{
scanf("%d", &b);
if(!ok) continue;//不要break,数据要读完
if (pos[b] == 0x7f7f7f7f)
ok = 0;
if (que(1, 1, pos[b]) == b)
{
update(1, pos[b]);
pos[b] = nxt[b];
}
else
ok = 0;
}
puts(ok ? "YES" : "NO");
}
return 0;
}
CodeForces 1187D Subarray Sorting的更多相关文章
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- codeforces 876 D. Sorting the Coins
http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- Codeforces 911E - Stack Sorting
911E - Stack Sorting 思路: 用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则 ...
- Codeforces 606-C:Sorting Railway Cars(LIS)
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
- codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C
思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
随机推荐
- sourceInsight下标题栏显示文件完整路径
用sourceInsight看代码方便,但是标题栏中不显示文件的完整路径,有时候就会很麻烦,做如下设置即可显示完整的路径 options -> preferences -> Display ...
- 在线cron表达式生成工具
http://cron.qqe2.com/ 名称 是否必须 允许值 特殊字符 秒 是 0-59 , - * / 分 是 0-59 , - * / 时 是 0-23 , - * / 日 是 1-31 , ...
- [转帖]JVM内存结构 VS Java内存模型 VS Java对象模型
JVM内存结构 VS Java内存模型 VS Java对象模型 https://www.hollischuang.com/archives/2509 Java作为一种面向对象的,跨平台语言,其对象.内 ...
- 极*Java速成教程 - (6)
Java高级特性 String String是Java中的字符串类型,字符串类型在内存中是一个不可变的对象.如果要对字符串对象进行修改,如果是较少的修改可以使用+运算符,Java会自动进行优化,但如果 ...
- C++类中的函数重载
1,本课程最初阶段就学习了函数重载,但是那时研究目标仅限于全局函数,到目前 为止我们学习了三种函数: 1,全局函数: 2,普通成员函数: 3,静态成员函数: 这三种不同类型的函数之间是否可以构成重载, ...
- centos7使用kubeadm搭建kubernetes集群
一.本地实验环境准备 服务器虚拟机准备 IP CPU 内存 hostname 192.168.222.129 >=2c >=2G master 192.168.222.130 >=2 ...
- 最长上升子序列(LIS) Easy
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given ...
- Nginx 入门了解
Nginx的产生 没有听过Nginx?那么一定听过它的"同行"Apache吧!Nginx同Apache一样都是一种WEB服务器.基于REST架构风格,以统一资源描述符(Unifor ...
- intellij idea 的快捷键方法
1.Ctrl+N按名字搜索类 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件,就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配,而且 ...
- python-xss攻击和单例模式
1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...