CF1187D Subarray Sorting
思路:
线段树好题。对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的更多相关文章
- CF1187D Subarray Sorting(神奇思路,线段树)
说实话,$2200$ 的题做不出来也有点丢脸了…… 当然要先判所有数出现次数相同. 首先区间排序就相当于交换相邻两个数,前面的数要大于后面的数才能交换. 然后就不会了…… 我们考虑 $b_1$ 到 $ ...
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- 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 ...
- Subarray Sorting (线段树)
题意:给你两个长度为 n 的序列 a 和 b , 可以对 a 进行 操作: 选择一段区间[ l, r ] ,使得序列a 在这段区间里 按升序排序. 可以对a 进行任意多次操作,问 a是否有可能变成b序 ...
- 【Edu 67】 补题记录
CF1187D. Subarray Sorting 想要把一个数x换到前面,x一定是小一点的值.由于B串是固定的,A串可调整,我们可以遍历B数组,对于B[i],找到对于在A数组的位子pos,判断1-p ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- Codeforces Educational Codeforces Round 67
目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...
- [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 ...
随机推荐
- 快速搭建FTP服务器
快速搭建一个本地的FTP服务器 如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试.第一步:配置IIS Web服务器1.1 控制面板中找到“程序”并打开 1.2 ...
- 打包完的rcp产品svn不储存密码问题
原因是缺少org.eclipse.core.runtime.compatibility.auth 这个依赖,需要添加到依赖中去 因为使用SVNKit的时候会去调eclipse这个api 详情见: ht ...
- python自动华 (十一)
Python自动化 [第十一篇]:Python进阶-RabbitMQ队列/Memcached/Redis 本节内容: RabbitMQ队列 Memcached Redis 1. RabbitMQ ...
- Windows服务启动时候报错1053
用.net 开发了一个C#语言的windows服务,在本地和测试环境,安装启动都正常,在新的线上环境报错,不能启动-报出-错误1053:服务没有及时响应启动或控制请求. 后来发现时线上.NET FRA ...
- 本地启动服务,两个进程分别监听两个端口,导致两个 URL 不同
问题描述: 本地启了两个服务:A(http://localhost:8001) B(http://localhost:8000),A 项目要怎么才能关联到 B 项目,也就是 A 项目请求怎么跳到 B ...
- CF46F Hercule Poirot Problem
题意: 有n个房间和m扇门,每扇门有且仅有一把钥匙 有k个人度过了两天,在第一天开始的时候所有的门都是关闭的,在第二天结束的时候,所有的门也都是关闭的 在这两天内,每个人可以执行如下操作若干次: 关上 ...
- 实战 Prometheus 搭建监控系统
实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ...
- web软件测试基础系统测试简化理论
系统测试点主要如下 1.系统测试基础-2.测试对象与测试级别-3.系统测试类型-4.系统测试方法-5.系统测试之软件测试质量. 1.系统测试:是尽可能彻底地检查出程序中的错误,提高软件系统的可靠性. ...
- pxe linux 0
操作环境:windows 实验环境:vmware workstation 实验要求:配置一台workstation机器(ip地址192.168.96.100 gateway 192.168.96.2 ...
- JavaWeb_(Mybatis框架)MyBatis整合Spring框架
MyBatis + Spring整合开发 a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory:b)使用Spring管理连接池.数据源等:c)将Dao/Mapper ...