5821 杭电OJ Ball

Ball

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 906 Accepted Submission(s): 532

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.

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.

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

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int a[1002],b[1002];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int c[1002]={0};
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n; i++)
scanf("%d",&b[i]);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(b[j]!=-1&&a[i]==b[j])
{
c[i]=j;
b[j]=-1;
break;
}
}
}
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
sort(c+x,c+y+1);
}
int t1=1;
for(int i=1; i<=n; i++)
{
if(c[i]!=i)
{
printf("No\n");
t1=0;
break;
}
}
if(t1==1)
printf("Yes\n");
}
return 0;
}



• 本文已收录于以下专栏：

杭电OJ 5821 Ball

Problem Description ZZX has a sequence of boxes numbered 1,2,...,n. Each box can contain at mos...
• qiaoermeng
• 2016年08月13日 10:41
• 107

杭电OJ题目 1000

• nxy006
• 2015年06月27日 20:49
• 4296

杭电OJ 2015

2016-10-22（已AC）#includeint main(void) { int n,m,j,k,a,a1; while(scanf("%d",&n)!=EOF) { ...
• ZHangFFYY
• 2016年11月02日 13:50
• 753

杭电oj 1010

• u011488028
• 2015年12月02日 09:38
• 2244

杭电oj题库分类

• zwj1452267376
• 2014年12月23日 21:52
• 2048

杭电OJ测试数据挖掘计划

• hoxily
• 2015年03月28日 16:31
• 930

杭电acm题目分类 非常详细

• lsgqjh
• 2015年03月13日 17:49
• 8769

杭电OJ（HDOJ）1005题：Number Sequence（规律递推）

• u014174811
• 2014年11月29日 19:06
• 1557

杭电oj（Java版）——

• agony_sun
• 2017年04月27日 12:02
• 289

杭电oj-1003-Max Sum

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum...
• yeyun666
• 2015年12月09日 14:39
• 787

举报原因： 您举报文章：5821 杭电OJ Ball 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)