Educational Codeforces Round 67 D. Subarray Sorting

传送门

题意;

给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得到\(b\)数组。

\(n\leq 3*10^5,a\leq n.\)

思路:

首先注意到任意次排序可以等价于任意次交换两个相邻的数,当且仅当前一个数不小于后面一个数。

我一开始想的是按权值从小到大来构造,但最终发现这条路走不通。

正解就是比较直接的思路,按位置一个一个来匹配。

对于一个\(b_i\),询问目前出现位置最早的\(a_j\),满足\(a_j=b_i\),当其能够移动过去,只要前面的数权值都不小于\(a_j\)即可。

因为我们匹配过后就要把\(a_j\)删去并且重新更新,所以不会出现\(j<i\)的情况。

以上权值线段树维护最小位置+vector存储位置即可解决。

代码如下:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int T;
int n;
int a[N], b[N];
vector <int> p[N];
int Min[N << 2];
void build(int o, int l, int r) {
Min[o] = INF;
if(l == r) return ;
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1|1, mid + 1, r);
}
void update(int o, int l, int r, int p, int v) {
if(l == r) {
Min[o] = v;
return ;
}
int mid = (l + r) >> 1;
if(p <= mid) update(o << 1, l, mid, p, v);
else update(o << 1|1, mid + 1, r, p, v);
Min[o] = min(Min[o << 1], Min[o << 1|1]);
}
int query(int o, int l, int r, int L, int R) {
if(l >= L && r <= R) return Min[o];
int ans = INF;
int mid = (l + r) >> 1;
if(L <= mid) ans = min(ans, query(o << 1, l, mid, L, R));
if(R > mid) ans = min(ans, query(o << 1|1, mid + 1, r, L, R));
return ans;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> T;
while(T--) {
cin >> n;
for(int i = 1; i <= n; i++) p[i].clear();
for(int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]].push_back(i);
}
for(int i = 1; i <= n; i++) cin >> b[i];
build(1, 1, n);
for(int i = 1; i <= n; i++) if(!p[i].empty()) {
reverse(p[i].begin(), p[i].end());
update(1, 1, n, i, p[i].back());
}
bool ok = true;
for(int i = 1; i <= n; i++) {
if(p[b[i]].empty()) {
ok = false;
break;
}
if(query(1, 1, n, 1, b[i]) < p[b[i]].back()) {
ok = false;
break;
}
p[b[i]].pop_back();
update(1, 1, n, b[i], (p[b[i]].empty() ? INF : p[b[i]].back()));
}
if(ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}

Educational Codeforces Round 67 D. Subarray Sorting的更多相关文章

  1. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  2. Codeforces Educational Codeforces Round 67

    目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...

  3. Educational Codeforces Round 67 (Rated for Div. 2)

    A 考虑之前选中没有一个的,那么结果就是\(min(n-s,n-t)\) 那么能选中的第一次就是这个结果\(+1\),但需要拥有两个 \((s>t)\)考虑一开始选不中\(t\),则但选中\(t ...

  4. Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】

    题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...

  5. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

  6. Educational Codeforces Round 69 D. Yet Another Subarray Problem

    Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...

  7. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  8. Educational Codeforces Round 69 D E

    Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. Golang(七)golang.org/x/time/rate 实现频率限制

    1. 源码阅读 整个包实现原理基于令牌桶算法:随时间以 1/r 个令牌的速度向容积为 b 个令牌的桶中添加令牌,有请求就取走令牌,若令牌不足则不执行请求或者等待 Allow 方法的调用链:lim.Al ...

  2. jenkins pipeline使用方式

    pipeline 使用 使用groovy的一种DSL语言,流程控制 pipeline脚本同其他脚本语言一样,从上到下顺序执行,它的流程控制取决于Groovy表达式,为jenkins用户提供了更巨大的灵 ...

  3. [C++基础] 数组、指针、内存篇

    一.数组 2.1 int a[2][2]= { {1}, {2,3} },则 a[0][1] 的值是多少? 二维数组的初始化一般有两种方式: 第一种方式是按行来执行,如int array\[2][3] ...

  4. spark listener

    最近在做一个需求,当spark程序在读数据或写数据时,将所读的条数或或所写的条数实时的展现出来,这里用到了SparkListener,sparklisten 可以获取spark 各个运行阶段的状态. ...

  5. 二分法构造AVL树

    public class ConvertSortedArrayToBinarySearchTree { public static TreeNode sortedArrayToBST(int[] nu ...

  6. SQLServer字符串与数字拼接

    1.使用cast‘’+cast(@ID as varchar) 2.使用LTrim‘’+LTrim(@ID) 感觉第二种方式代码简单,但是可读性不好.

  7. Replication:distribution 中一直在运行 waitfor delay @strdelaytime 语句

    Replication 自动创建来一个 Job:Replication monitoring refresher for distribution,这个Agent执行一个sp: dbo.sp_repl ...

  8. java运算符的优先级别

    一.运算符的优先级 运算符按照优先级别的高低排序分别是:自加/减运算符. 算术运算符.比较运算符.逻辑运算符.赋值运算符.具体请参考下表: 顺序 运算符 1. 括号,如 ( ) 和 [ ] 2. 一元 ...

  9. c#按指定长度分解数组

    在操作数据库时,我们需要注意一点,就是in查询的参数不能超过1000个,否则会报错,所以我们在进行in查询的时候需要对参数数量进行控制: 用于分解数组的扩展方法: /// <summary> ...

  10. 创建.net framework webapi出现“Web 服务器被配置为不列出此目录的内容。”错误

    接了一个新任务,要求写一个web api.于是我创建了一个.net framework的web api,结果在运行的时候,出现了以下页面: 解决方法: 在web.config文件中添加<dire ...