当前位置: 首页 > article >正文

pSort

pSort

题面翻译

题目描述

给定一个长度为 n n n 的数列 { a n } \{a_n\} {an},初始时 { a n } = { 1 , 2 , … , n } \{a_n\} = \{1, 2, \dots, n\} {an}={1,2,,n}。位置 i i i 上的数可以和位置 i ± d i i \pm d_i i±di 上的数交换。给定一个 1 ∼ n 1 \sim n 1n 的全排列,问初始的数列可否交换成给定的形式。

输入格式

第一行一个整数 n n n

第二行 n n n 个互不相同的整数表示目标数列。

第三行 n n n 个整数表示 d 1 , d 2 , … , d n d_1, d_2, \dots, d_n d1,d2,,dn

输出格式

如果能交换到给定样式,输出 YES,否则输出 NO

数据范围

1 ≤ n ≤ 100 1 \le n \le 100 1n100

题目描述

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

The favourite 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.

输入格式

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 $ — favourite numbers of the cells.

输出格式

If the given state is reachable in the described game, output YES, otherwise NO.

样例 #1

样例输入 #1

5
5 4 3 2 1
1 1 1 1 1

样例输出 #1

YES

样例 #2

样例输入 #2

7
4 3 5 1 2 7 6
4 6 6 1 6 6 1

样例输出 #2

NO

样例 #3

样例输入 #3

7
4 2 5 1 3 7 6
4 6 6 1 6 6 1

样例输出 #3

YES

思路:对于每一个数,想要到达它原来的位置的话,我们应该能够确保它们在一个连通块内,只有这样才能够到达目标位置,因此我们可以想到利用并查集,最后判断i与fa[a[i]]是否一样即可

AC代码:

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int MOD = 998244353;
const int N = 4e5 + 10;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};


int fa[110];
int a[110];
int d[110];
int finds(int x)
{
	if(x != fa[x]) fa[x] = finds(fa[x]);
	return fa[x];
}

void un(int x, int y)
{
	int o1 = finds(x), o2 = finds(y);
	if(o2 != o1) 
	{
		fa[o2] = fa[o1];
	}
}
int main()
{
	int n;
		cin >> n;
		for(int i = 1; i <= n; i ++) fa[i] = i;
		for(int i = 1; i <= n; i ++) cin >> a[i];
		for(int i = 1; i <= n; i ++) cin >> d[i];
		for(int i = 1; i <= n; i ++)
		{
			if(i > d[i]) un(i, i - d[i]);
			if(i + d[i] <= n) un(i, i + d[i]);
		}
		int f = 0;
		for(int i = 1; i <= n; i ++)
		{
			if(finds(i) != finds(a[i]))
			{
				f = 1;
				break;
			}
		}
		if(f) puts("NO");
		else puts("YES");
}

http://www.kler.cn/news/316837.html

相关文章:

  • < 微积分Calculus >
  • 【自学笔记】支持向量机(3)——软间隔
  • MySQL--导入SQL文件(命令行导入)
  • 马尔科夫蒙特卡洛_吉布斯抽样算法(Markov Chain Monte Carlo(MCMC)_Gibbs Sampling)
  • 小程序服务零工市场
  • 基于51单片机的矿井安全检测系统
  • SpringBoot 整合 apache fileupload 轻松实现文件上传与下载(通用版)
  • LeetCode 260. 只出现一次的数字 III
  • 用Python提取PowerPoint演示文稿中的音频和视频
  • CVE-2024-46101
  • 0.初始化项目(Vue2升级到Vue3)
  • 物联网新闻2024.09.16-2024.09.22
  • flume系列之:出现数据堆积时临时增大sink端消费能力
  • LAMP环境搭建记录:基于VM的Ubuntu虚拟机
  • 编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MSVC版)
  • (学习记录)使用 STM32CubeMX——GPIO引脚输入配置
  • 实时数据的处理一致性
  • JavaScript(JS)学习笔记 3(DOM简介 事件简介 元素修改 节点操作 事件操作)
  • MySQL:事务隔离级别
  • Kubernets基础-包管理工具Helm详解
  • 计算机组成原理==初识二进制运算
  • Redisson分布式锁主从一致性问题
  • CentOS修改主机名
  • 【已解决】如何使用JAVA 语言实现二分查找-二分搜索折半查找【算法】手把手学会二分查找【数据结构与算法】
  • 用Qt 对接‌百度AI平台
  • 响应式流规范
  • 胤娲科技:谷歌DeepMind祭出蛋白质设计新AI——癌症治疗迎来曙光
  • DoppelGanger++:面向数据库重放的快速依赖关系图生成
  • JavaScript语法特点
  • linux 使用mdadm 创建raid0 nvme 磁盘