题解 [CF1672D] Cyclic Rotation
一眼感觉比较神,但性质还是很好找的
发现可以倒着扫 b 数组,需要某个数的话倒着从 a 中取
是需要的颜色就取,否则需要换到后面去
如果后面不需要它则无解
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define ll long long
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n;
int a[N], b[N];
map<int, int> mp;
signed main()
{
int T=read();
while (T--) {
n=read();
mp.clear();
for (int i=1; i<=n; ++i) a[i]=read();
for (int i=1; i<=n; ++i) b[i]=read();
int p1, p2;
for (p1=n,p2=n; p2; --p2) {
int k=1;
while (p2 && b[p2-1]==b[p2]) --p2, ++k;
while (p1>0 && a[p1]!=b[p2]) {
if (mp[a[p1]]<0) ++mp[a[p1--]];
else {puts("NO"); goto jump;}
}
if (p1<=0) {puts("NO"); goto jump;}
else mp[a[p1--]]-=k-1;
}
while (p1>0) ++mp[a[p1--]];
for (auto it:mp) if (it.second) {puts("NO"); goto jump;}
puts("YES");
jump: ;
}
return 0;
}
浙公网安备 33010602011771号