思路:

线段树好题。对a数组中的每个元素从左到右依次操作,判断最终是否能够转化成b数组。在此过程中使用线段树维护区间最小值判断是否能够进行合法操作。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int N = , INF = 0x3f3f3f3f;
deque<int> d[N];
int a[N], b[N], tree[N * ]; void build(int num, int l, int r)
{
if (l == r) { tree[num] = a[l]; return; }
int m = l + r >> ;
build(num << , l, m);
build(num << | , m + , r);
tree[num] = min(tree[num << ], tree[num << | ]);
} void update(int num, int l, int r, int x, int y)
{
if (l == r) { tree[num] = y; return; }
int m = l + r >> ;
if (x <= m) update(num << , l, m, x, y);
else update(num << | , m + , r, x, y);
tree[num] = min(tree[num << ], tree[num << | ]);
} int query(int num, int l, int r, int x, int y)
{
if (x <= l && y >= r) return tree[num];
int m = l + r >> ;
int ans = INF;
if (x <= m) ans = min(ans, query(num << , l, m, x, y));
if (y >= m + ) ans = min(ans, query(num << | , m + , r, x, y));
return ans;
} int main()
{
int t, n; cin >> t;
while (t--)
{
cin >> n;
for (int i = ; i <= n; i++) d[i].clear();
for (int i = ; i <= n; i++) { cin >> a[i]; d[a[i]].push_back(i); }
build(, , n);
for (int i = ; i <= n; i++) cin >> b[i];
bool flg = true;
int i = , j = ;
while (i <= n && j <= n)
{
while (i <= n && a[i] == INF) i++;
if (a[i] == b[j]) { d[a[i]].pop_front(); i++; j++; }
else if (a[i] < b[j]) { flg = false; break; }
else
{
if (d[b[j]].empty()) { flg = false; break; }
else
{
int t = d[b[j]].front();
int minn = query(, , n, i, t);
if (minn < a[t]) { flg = false; break; }
else
{
update(, , n, t, INF); a[t] = INF;
d[b[j]].pop_front();
j++;
}
}
}
}
cout << (flg ? "YES" : "NO") << endl;
}
return ;
}

CF1187D Subarray Sorting的更多相关文章

  1. CF1187D Subarray Sorting(神奇思路,线段树)

    说实话,$2200$ 的题做不出来也有点丢脸了…… 当然要先判所有数出现次数相同. 首先区间排序就相当于交换相邻两个数,前面的数要大于后面的数才能交换. 然后就不会了…… 我们考虑 $b_1$ 到 $ ...

  2. Educational Codeforces Round 67 D. Subarray Sorting

    Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...

  3. 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 ...

  4. Subarray Sorting (线段树)

    题意:给你两个长度为 n 的序列 a 和 b , 可以对 a 进行 操作: 选择一段区间[ l, r ] ,使得序列a 在这段区间里 按升序排序. 可以对a 进行任意多次操作,问 a是否有可能变成b序 ...

  5. 【Edu 67】 补题记录

    CF1187D. Subarray Sorting 想要把一个数x换到前面,x一定是小一点的值.由于B串是固定的,A串可调整,我们可以遍历B数组,对于B[i],找到对于在A数组的位子pos,判断1-p ...

  6. Educational Codeforces Round 67

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

  7. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  8. Codeforces Educational Codeforces Round 67

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

  9. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

随机推荐

  1. 转 java 中int String类型转换

    转自licoolxue https://blog.csdn.net/licoolxue/article/details/1533364 int -> String int i=12345;Str ...

  2. FastDFS+Nginx+Module

    1.安装libevent wget  https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.g ...

  3. BZOJ 1299: [LLH邀请赛]巧克力棒 【SG函数/博弈分析/高斯消元】

    因为太懒,放个博客 我只写了O(2n)O(2^n)O(2n)的 CODE #include <cstdio> int n, x[15]; int main () { for(int T = ...

  4. 016_编写脚本快速克隆 KVM 虚拟机

    #!/bin/bash#本脚本针对 RHEL7.2 或 Centos7.2#本脚本需要提前准备一个 qcow2 格式的虚拟机模板,名称为/var/lib/libvirt/images/.rh7_tem ...

  5. unix/linux 进程间文件锁

    转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程 ...

  6. redis详解之cluster模式部署

    一.环境说明 1.Operation OS:CentOS7.22.ruby version >= 2.2.23.openssl zlib gcc>=4.8.5 二.开始部署 1.安装rub ...

  7. jenkins自动化部署gitlab上maven程序

    部署流程:将代码从gitlab上拉取下来,使用maven打包,将打包后的jar通过ssh发送到服务器上,运行jar程序 注意:本文需要安装一些插件Publish Over SSH 1.新建任务 在主页 ...

  8. easyui-combobox和C标签判断回显

    <td width="40%"> <select class="easyui-combobox" id="work_property ...

  9. fluent中隐藏模型的开启【转载】

    转载自:http://blog.sina.com.cn/s/blog_5fd791530100d5ic.html fluent中设置了一些隐藏模型,普通的用户界面是没有相关选项的,必须用相关命令开启. ...

  10. 启动maven项目时报错Failed to start component [StandardEngine[Tomcat]]: A child container failed during start

    详细错误信息:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on ...