10-排序6 Sort with Swap(0, i) (25point(s))
10-排序6 Sort with Swap(0, i) (25point(s))
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *)
is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤10^5) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.
Output Specification:
For each case, simply print in a line the minimum number of swaps need to sort the given permutation.
Sample Input:
10
3 5 7 2 6 4 9 0 8 1
Sample Output:
9
N 个数字的排列由若干个独立的环构成
。每一次都是和 0 做 swap,如果这个环里面有 0,长度是 count,那么每次都找和 0 值这个元素的下标相同的元素,做一次 swap。对于这个环本来就有 0,只需要做 count - 1
次 swap。若这个环里面没有 0,就要把 0 先 swap 进去,然后再归位所以比环里有 0 的多 2 次,需要 count + 1
次 swap。如果 count == 0
那这个元素不用和谁做 swap。
#include <stdio.h>
#define N 100000
/* 每次都和 0 做 swap,一次归位一个数字 */
int main(int argc, char const *argv[])
{
int a[N];
int visited[N] = {0};
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int sum = 0; /* 总共需要多少次 swap */
for (i = 0; i < n; i++) {
if (!visited[i]) {
visited[i] = 1;
int index = i;
int count = 0; /* 环的元素个数,0 个不用做 swap */
int next;
while (a[index] != index) {
visited[index] = 1;
count++;
next = a[index];
a[index] = index;
index = next;
}
if (count != 0) {
sum = index == 0 ? sum + count - 1 : sum + count + 1;
}
// printf("index = %d, count = %d\n", index, count);
}
}
printf("%d\n", sum);
return 0;
}
10-排序6 Sort with Swap(0, i) (25point(s))的更多相关文章
- PAT 1067. Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
- Pat1067:Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- PTA 10-排序6 Sort with Swap(0, i) (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i) (25分) Given a ...
- 7-16 Sort with Swap(0, i)(25 分)
7-16 Sort with Swap(0, i)(25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...
- 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
- 1067 Sort with Swap(0, i) (25 分)
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
随机推荐
- 使用Logstash把MySQL数据导入到Elasticsearch中
总结:这种适合把已有的MySQL数据导入到Elasticsearch中 有一个csv文件,把里面的数据通过Navicat Premium 软件导入到数据表中,共有998条数据 文件下载地址:https ...
- CentOS 7.7系统安装Redis 6.0.3
前提操作 避免出现如下的错误 yum -y install gcc tcl yum -y install centos-release-scl yum -y install devtoolset-9- ...
- MongoDB 分片集群的用户和权限一般操作步骤
步骤总结: 按照mongos路由.配置副本集服务,分片副本集服务的先后顺序关闭所有节点服务 创建副本集认证的key文件,复制到每个服务所在目录 修改每个服务的配置文件,增加参数 启动每个服务 创建账号 ...
- 初等数论学习笔记 III:数论函数与筛法
初等数论学习笔记 I:同余相关. 初等数论学习笔记 II:分解质因数. 1. 数论函数 本篇笔记所有内容均与数论函数相关.因此充分了解各种数论函数的名称,定义,符号和性质是必要的. 1.1 相关定义 ...
- Pytorch及Yolov5环境配置及踩坑
Pytorch及Yolov5环境配置及踩坑 1.何为Yolov5 yolo是计算机视觉方面用来进行目标检测的一个十分出名的开源框架,我搜不到官方的对此概括性的定义,但实际上也没什么必要,更重要的是会使 ...
- LOJ139 树链剖分
题目 感觉这已经不能说是模板了吧...... 解析: 难点在于换根后对子树进行的操作,设rt为当前根节点,u为操作子树: u=rt时,就是对整棵树操作,没事么好说的. rt不在u的子树范围内,操作对象 ...
- P1399 [NOI2013] 快餐店 方法记录
原题题面P1399 [NOI2013] 快餐店 题目描述 小 T 打算在城市 C 开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小 T 希望快餐店的地址选在离最 ...
- SpringBoot后端接口项目
创建SpringBoot项目 项目目录 实体类 点击查看代码 package com.bai.entity; import com.baomidou.mybatisplus.annotation.Id ...
- jstl的使用 转发和重定向(做项目遇到的一些问题总结)
文章目录 1.jstl的使用 2.转发和重定向 3.shiro 4.spring是什么? 5.对AOP的理解 6.标签通常和标签一起使用 7.springmvc中的form表单 7.1 form标签 ...
- Vue3 SFC 和 TSX 方式自定义组件实现 v-model
1 v-model 1.1 理解 v-model v-model 是 vue3 中的一个内置指令,很多表单元素都可以使用这个属性,如 input.checkbox 等,咱可以在自定义组件中实现 v-m ...