Educational Codeforces Round 67 D. Subarray Sorting
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的更多相关文章
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- Codeforces Educational Codeforces Round 67
目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...
- Educational Codeforces Round 67 (Rated for Div. 2)
A 考虑之前选中没有一个的,那么结果就是\(min(n-s,n-t)\) 那么能选中的第一次就是这个结果\(+1\),但需要拥有两个 \((s>t)\)考虑一开始选不中\(t\),则但选中\(t ...
- Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】
题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- [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 ...
- Educational Codeforces Round 69 D E
Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- 常用war包插件
<build> <resources> <resource> <directory>src/main/java</directory> &l ...
- netcore容器与配置文件操作
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Micro ...
- 【06月04日】A股滚动市盈率PE历史新低排名
2010年01月01日 到 2019年06月04日 之间,滚动市盈率历史新低排名. 上市三年以上的公司,2019年06月04日市盈率在300以下的公司. 1 - 阳光照明(SH600261) - 历史 ...
- 二分法构造AVL树
public class ConvertSortedArrayToBinarySearchTree { public static TreeNode sortedArrayToBST(int[] nu ...
- Tp中使用Xunsearch
可以将安装目录下的核心代码拷贝到Vendor中. 命名为xunsearch. 然后引入使用. vendor('xunsearch.lib.XS'); $xs = new \XS('demo'); // ...
- Springboot Actuator之十:actuator中的audit包
前言这篇文章我们来分析一下org.springframework.boot.actuate.security,org.springframework.boot.actuate.audit中的代码,这2 ...
- 软件——解决Modelsim10.1d窗口不停弹出问题(一直弹窗)
博主在编写Verilog HDL时需要用到Modelsim,于是博主便安装了Modelsim10.1d,然后兴高采烈打开准备跑仿真时,打开软件发现Modelsim10.1d的各种窗口在不停弹出,终止进 ...
- FMC与FPGA双口ram通讯
硬件环境:ARM+FPGA通过FMC互联,STM32F767和 EP4CE15F23I7 FMC设置,STM的系统时钟HCLK为216MHz /* FMC initialization functio ...
- Markdown_word_chain_test2222
# Flutter ![Flutter logo][] [![Gitter Channel][]][Gitter badge] [![Build Status - Cirrus][]][Build ...
- TCP和UDP的差异
TCP建立连接 如果有人问TCP如何建立连接?大部分的回答都是三次握手成功,就建立连接了. 那么握手的目的是什么呢? 1)告知对方自己的序号 2)初始化资源 例如以下握手案例,握手两次由客户端主动发起 ...