跳到正文

题解

CF1672D Cyclic Rotation

rbjwnb约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年9月5日
收录
2025年12月16日
更新
2026年11月21日
篇幅
约 2 分钟
题目
CF1672D

版本

  1. v2当前
  2. v1

CF1672D Cyclic Rotation 题解

  1. 我们发现题目所述操作只能使得一个数字往后移动;

  2. 可得出若 aa 数组想变为 bb 数组那么只需从后往前遍历a数组,当遇到与 bb 数组相同的连续的数字且可以填的时候,填进去,若最后 bb 数组能被填完则可以;

  3. 为了方便处理,先预处理 bb 数组,将一段连续的相同序列使用 pair 存储,first 代表权值,second 代表数量。

C++
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define pp pop_back
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e6+5, M = 1e6+5, MOD = 998244353;
int n, a[N], b[N];
int cnt[N];
void solve() {
	cin >> n;
	for(int i = 1; i <= n; i ++) 
		cin >> a[i];
	for(int i = 1; i <= n; i ++) 
		cin >> b[i];
	for(int i = 1; i <= n; i ++)
		cnt[i] = 0;
	int res = 1;
	vector<PII> v;
	for(int i = 2; i <= n; i ++) {
		if(b[i] == b[i-1]) res ++;
		else {
			v.pb({b[i-1], res});
			res = 1;
		}
	}
	v.pb({b[n], res});
	int p = 0;
	for(int i = 1; i <= n; i ++) {
		cnt[a[i]] ++;
		if(a[i] == v[p].x && cnt[a[i]] >= v[p].y) {
			cnt[a[i]] -= v[p].y;
			p ++;
		}
	}
	if(p == v.size()) cout << "YES\n";
	else cout << "NO\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	int t = 1;
	cin >> t;
	while(t --) solve();
	
	return 0;
}
原文