Ball
.
.
解法:贪心。因为相同的数字相互之间位置不交换更优,对于序列a的第一个1就期望移到序列b的第一个1的位置,一次类推。那么先把b的序列固定顺序,然后在赋给a序列每个数期望的位置。然后对于每次给的l,r就把l~r排一次序,看最后是不是1~n就行了。
.
.
#include <cstdio>
#include <algorithm>
using namespace std;
struct p {
int pos, num;
} a[1010], b[1010];
int aa[1010], bb[1010], go[1010], l[1010], r[1010];
int cnt;
bool cmp1(p a, p b)
{
if (a.num == b.num) return a.pos < b.pos;
return a.num < b.num;
}
bool cmp2(p a, p b)
{
return a.pos < b.pos;
}
int main()
{
int n, m, t;
scanf("%d", &t);
while (t--)
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i].num);
a[i].pos = i;
}
for (int i = 1; i <= n; i++)
{
scanf("%d", &b[i].num);
b[i].pos = i;
}
for (int i = 1; i <= m; i++)
{
scanf("%d %d", &l[i], &r[i]);
}
sort(a + 1, a + 1 + n, cmp1);
sort(b + 1, b + 1 + n, cmp1);
bool ok = true;
for (int i = 1; i <= n; i++)
{
if (a[i].num != b[i].num)
{
ok = false;
break;
}
}
for (int i = 1; i <= n; i++) b[i].num = i;
for (int i = 1; i <= n; i++) a[i].num = i;
sort(a + 1, a + 1 + n, cmp2);
sort(b + 1, b + 1 + n, cmp2);
if (ok)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
if (b[i].num == a[j].num)
{
go[j] = i; break;
}
}
for (int i = 1; i <= m; i++)
{
int ll = l[i], rr = r[i];
sort(go + ll, go + rr + 1);
}
for (int i = 1; i <= n; i++)
{
if (go[i] != i)
{
ok = false; break;
}
}
if (ok) printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
}