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

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");
    }
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值