HDU-5821-Ball

原题
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 ii-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
题意:
有两个相同长度且长度为n的数列a,b,现有m步操作,每一次操作都可以将left[i]~right[i]区间内的所有元素提取出来随意改变顺序重新放回。问能否在这m步操作后两个数列相同。
题解:
这道题目带有贪心思想,我们可以遍历所有的a[i]在b数列中找到第一个与他相等的元素就是他要换的元素,而且这样可以保证更容易换到正确的位置(因为所选区间是连续的),用mp数组记录下来,这样我们每一次操作只需要对区间内从大到小排序即可,因为每一个元素要交换的都是离自己最近的一个,如果操作完成后,所有的mp[i]==i都成立,则证明是可以完成的,反之则不可以。
更多细节见代码:
附上AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[1005], b[1005];
int mp[1005];
int vis[1005];
int l,r;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis, 0, sizeof(vis));//多个案例需要每次重置
        memset(mp, 0, sizeof(mp));
        int n,m;
        cin>>n>>m;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        for(int i = 1; i <= n; i++)
        {
            cin>>b[i];
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(vis[j] != 0)
                    continue;
                if(b[j] == a[i])//在b数组中找到第一个与a[i]相等的元素
                {
                    mp[i] = j;//记录需要交换的位置
                    vis[j]++;//标记b数组中的此元素已被绑定
                    break;
                }
            }
        }

        for(int i = 0; i < m; i++)
        {
            cin>>l>>r;
            sort(mp+l, mp+r+1);//由于mp数组记录的是位置,所以每一次排序都能够使区间内的元素距离想要换的位置更近
        }
        bool ans = 0;
        for(int i = 1; i <= n; i++)
        {
            if(mp[i] != i)//检验是否都换到和a相同的位置上
            {
                ans = 1;
                break;
            }
        }
        if(ans)
            cout << "No" << endl;
        else
            cout << "Yes" <<endl;
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/83088479
今日推荐