Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1080 Accepted Submission(s): 649
Problem Description
ZZX has a sequence of boxes numbered
1,2,...,n
. Each box can contain at most one ball.
You are given the initial configuration of the balls. For 1≤i≤n , if the i -th box is empty then a[i]=0 , otherwise the i-th box contains exactly one ball, the color of which is a[i], a positive integer. Balls with the same color cannot be distinguished.
He will perform m operations in order. At the i-th operation, he collects all the balls from boxes l[i],l[i]+1,...,r[i]-1,r[i], and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball)
He wants to change the configuration of the balls from a[1..n] to b[1..n] (given in the same format as a[1..n]), using these operations. Please tell him whether it is possible to achieve his goal.
You are given the initial configuration of the balls. For 1≤i≤n , if the i -th box is empty then a[i]=0 , otherwise the i-th box contains exactly one ball, the color of which is a[i], a positive integer. Balls with the same color cannot be distinguished.
He will perform m operations in order. At the i-th operation, he collects all the balls from boxes l[i],l[i]+1,...,r[i]-1,r[i], and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball)
He wants to change the configuration of the balls from a[1..n] to b[1..n] (given in the same format as a[1..n]), using these operations. Please tell him whether it is possible to achieve his goal.
Input
First line contains an integer t. Then t testcases follow.
In each testcase: First line contains two integers n and m. Second line contains a[1],a[2],...,a[n]. Third line contains b[1],b[2],...,b[n]. Each of the next m lines contains two integers l[i],r[i].
1<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000.
0<=a[i],b[i]<=n.
1<=l[i]<=r[i]<=n.
In each testcase: First line contains two integers n and m. Second line contains a[1],a[2],...,a[n]. Third line contains b[1],b[2],...,b[n]. Each of the next m lines contains two integers l[i],r[i].
1<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000.
0<=a[i],b[i]<=n.
1<=l[i]<=r[i]<=n.
Output
For each testcase, print "Yes" or "No" in a line.
Sample Input
5 4 1 0 0 1 1 0 1 1 1 1 4 4 1 0 0 1 1 0 0 2 2 1 4 4 2 1 0 0 0 0 0 0 1 1 3 3 4 4 2 1 0 0 0 0 0 0 1 3 4 1 3 5 2 1 1 2 2 0 2 2 1 1 0 1 3 2 4
Sample Output
No No Yes No Yes
先给出一串数列,表示初始状态,数字0代表盒子里没球,>=1则代表有一个球,数字不同表示颜色不同,再给出一串数列表示目标状态,给出m次操作,每次操作给出一个左右范围,在这次操作里,可以将左右范围里面的球全部拿出来再随意的放回去,问m次操作之后能不能得到目标状态.
看了别人的解释,想了挺长时间才明白为啥这是一道贪心排序,首先是用一个数组去记录初始状态里面的数字在目标状态里出现位置的序号,并且尽量让在前面的为小,每次操作就让给出范围里面的数字从小到大排,这样自然序号小的就往前面去了,序号大的就往后面去了,这样每个球就往自己的目标位置靠近了,m次后来判断是否达到了目标状态,这方法真的是十分巧妙..
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main(){
int T;
cin>>T;
while(T--)
{
int num,op;
int start[1111];
int next[1111];
cin>>num>>op;
int i,j;
for(i=1;i<=num;i++)
cin>>start[i];
for(i=1;i<=num;i++)
cin>>next[i];
int now[1111];
bool vis[1111];
memset(now,0,sizeof(now));
memset(vis,false,sizeof(vis));
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
{
if(vis[j]) continue;
if(next[j]!=start[i]) continue;
now[i]=j;
vis[j]=true;
break;
}
}
while(op--)
{
int left,right;
cin>>left>>right;
sort(now+left,now+right+1);
}
int flag = 1;
for(i=1;i<=num;i++)
{
if(now[i]!=i)
{
flag = 0;
break;
}
}
if(flag==1)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}