Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5821
BallTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total Submission (s): 515 Accepted Submission (s): 309
Problem DESCRIPTIONZZX has a sequence of boxes numbered 1,2,...,N . Each box can contain at the 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 is distinguished.
He'll 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 BAC K to these boxes. (Note that each box should always contain at the 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 the SE operations. Please tell him whether it's possible to achieve he goal.
Inputfirst line contains an integer t. Then T testcases follow.
In each testcase:first line contains the 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 and 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.
Outputfor each testcase, print "Yes" or "No" in a line.
Sample Input
54 10 0 1 10 1 1 11 44 10 0 1 10 0 2 21 44 21 0 0 00 0 0 11 33 44 21 0 0 00 0 0 13 41 35 21 1 2 2 02 2 1 1 01 32 4
Sample Output
Nonoyesnoyes
Author Learning Army Secondary School
Source2016 multi-university Training Contest 8Main topic:give two lengths for aNthe arrayaand theb, and then givenmoperation, each time a givenLand theR, each time you can put[L,r]the number of the switch position, ask whether the conversion to makea arraybecomeB Array.
Problem-solving ideas: Using a structure to store a array, values and subscripts, the subscript is stored in a array corresponding to the subscript after the B array. And then in the array of a to sort the subscript, so that the value of a array will be changed with the subscript, as far as possible to the same value A and b corresponding, and finally in the sweep to see two arrays of the same position number is the same, the same output yes, otherwise output No.
See the code.
#include <iostream> #include <cstdio> #include <algorithm>using namespace std; #define N 1010struct node{int a,i;//value and subscript} s[n];int B[n];bool cmp (node A,node b) {return A.I<B.I;} int main () {int t; scanf ("%d", &t); while (t--) {int n,m,flag=1; scanf ("%d%d", &n,&m); for (int i=0; i<n; i++) {scanf ("%d", &s[i].a); S[i].i=-1; } for (int i=0; i<n; i++) {scanf ("%d", &b[i]); for (int j=0; j<n; J + +) {if (s[j].a==b[i]&&s[j].i==-1) { S[j].i=i; Break }}} int l,r; for (int i=0; i<m; i++) {scanf ("%d%d", &l,&r); Sort (s+l-1,s+r,cmp);//Sort according to S subscript} for (int i=0; i<n; i++) {if (B[I]!=S[I].A) {flag=0; Break }} if (flag==1) printf ("yes\n"); else printf ("no\n"); } return 0;}
Hdu 5821 Ball (multi-university Training Contest 8--greedy + sort)