hrbust 1748Sort the Array【传递闭包+Floyd】

来源:互联网 发布:php高级工程师证书 编辑:程序博客网 时间:2024/05/17 19:19

Sort the Array

Time Limit: 1000 MS

Memory Limit: 65536 K

 

Total Submit: 10(8 users)

Total Accepted: 8(8 users)

Rating: 

Special Judge: No

 

Description

One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to its ordinal number (starting from 1). Also each cell determined its favorite number. On its move i-th cell can exchange its value with the value of some other j-th cell, if |i - j| = di, where di is a favorite number of i-th cell. Cells make moves in any order; the number of moves is unlimited.

The favorite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.

Input

There are multiple test cases.

For each test case:

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favorite numbers of the cells.

Output

If they can exchange the house ID to form the given permutation output YES. Otherwise output NO.

Sample Input

5

5 4 3 2 1

1 1 1 1 1

7

4 3 5 1 2 7 6

4 6 6 1 6 6 1

7

4 2 5 1 3 7 6

4 6 6 1 6 6 1

Sample Output

YES

NO

YES

 

题目大意:
有一个序列,其长度为n。对于长度为n的这个序列,每个数都有一个可交换条件:|i-j|=di,如果满足这个绝对值距离,那么在第i个位子的数就可以和第j个位子上的数交换,对于每一个位子上的数可以无限次的进行此类操作,问能否最终交换出来一个序列,使得其从1-n依次排列。


思路:


1、设定map【i】【j】表示数字i和数字j可以交换位子。那么如果数字i能够和第i个位子上的数交换,那么这个数就能到其应该到的位子。


3、建图:对应一个数字看成一个点,一个数字最多连出去两条无向边:map【a【i】】【a【i+pos】】=1(1<=i+pos<=n)、map【a【i】】【a【i-pos】】=1(1<=i-pos<=n)


4、对建成的图,如果有:map【j】【i】==1&&map【i】【k】==1那么就有:map【j】【k】=1;然后我们跑一遍Floyd求其传递闭包。


6、题目目的:if(map【i】【tmp【i】】==1)【1<=i<=n】,其中tmp【i】表示在位子i的数。如果满足,输出YES,否则输出NO


Ac代码:


#include<stdio.h>#include<string.h>using namespace std;int a[1050];int dis[1050];int map[1050][1050];int tmp[1050];int main(){    int n;    while(~scanf("%d",&n))    {        memset(tmp,0,sizeof(tmp));        memset(map,0,sizeof(map));        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            tmp[a[i]]=i;        }        for(int i=1;i<=n;i++)        {            scanf("%d",&dis[i]);        }        for(int i=1;i<=n;i++)        {            map[a[i]][a[i]]=1;            int pos=i+dis[i];            if(pos<=n&&pos>=1)            {                map[a[i]][a[pos]]=1;                map[a[pos]][a[i]]=1;            }            pos=i-dis[i];            if(pos<=n&&pos>=1)            {                map[a[i]][a[pos]]=1;                map[a[pos]][a[i]]=1;            }        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                for(int k=1;k<=n;k++)                {                    if(map[j][i]==1&&map[i][k]==1)                    {                        map[j][k]=1;                    }                }            }        }        int flag=1;        for(int i=1;i<=n;i++)        {            if(map[i][tmp[i]]==0)flag=0;        }        if(flag==1)        {            printf("YES\n");        }        else printf("NO\n");    }}




0 0
原创粉丝点击