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))的更多相关文章

  1. 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 ...

  2. Pat1067:Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

  7. PAT_A1067#Sort with Swap(0, i)

    Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...

  8. 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 ...

  9. PAT1067. Sort with Swap(0, *) (25) 并查集

    PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...

随机推荐

  1. k8s-ingress配置websocket支持

    具体来说,使用的ingress-controller不一样,有关的设置也不太一样 Kubernetes Ingress Controller (k8s官方) 参考 http://github.com/ ...

  2. Elasticsearch之集群角色类型

    角色划分 在Elasticsearch中,有很多角色,常用的角色有如下: Master Node:主节点 Master eligible nodes:合格节点 Data Node:数据节点 Coord ...

  3. Elastic:Sense chrome - 在 chrome 浏览器中操作 Elasticsearch 的插件

    文章转载自:https://elasticstack.blog.csdn.net/article/details/114533904 Sense 是我们现在所说的 Kibana 控制台 UI 的第一个 ...

  4. Visual Studio 2022 开发 STM32 单片机 - 环境搭建点亮LED灯

    安装VS2022社区版软件 选择基础的功能就好 安装VisualGDB软件(CSDN资源) 按照提示一步一步安装就好 VisualGDB激活软件(CSDN资源) 将如下软件放在VisualGDB的安装 ...

  5. PAT (Basic Level) Practice 1009 说反话 分数 20

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小 ...

  6. P7476 苦涩 题解

    Link 一道很好的复杂度均摊题目. 只需要考虑删除操作时的时间复杂度.保证复杂度的重点之一是精确定位到所有包含最大值的区间,即不去碰多余的区间.每次删除操作会删除若干个整个区间,以及至多两个区间被删 ...

  7. day04-MySQL常用函数01

    5.MySQL常用函数 5.1合计/统计函数 5.1.1合计函数-count count 返回行的总数 Select count(*)|count (列名) from table_name [WHER ...

  8. P3402 可持久化并查集

    P3402 通过主席树维护不同版本的并查集,注意要采用按秩合并的方式,路径压缩可能会爆. 1 #include <bits/stdc++.h> 2 using namespace std; ...

  9. 空 Maven项目转成 Web项目 & SpringMVC调用其他 Module中的方法可能会遇到的小问题

    SpringMVC调用其他 模块内的方法的 坑 下次别在阴沟里翻船啦.. 一共花费 4个小时,解决项目中的这个问题 OMG 1. 首先是 Maven新建工程 一般使用 Maven都是先创建 空工程 当 ...

  10. Python学习笔记----列表、元组和字典的基础操作

    文章目录 一.列表的基本操作 1.1 修改列表中的某个数据 1.2 获取某个元素的返回值(返回的是索引) 1.3 在列表中插入新的元素 1.4 删除列表中的元素 1.5 +和* 二.内置的函数和方法 ...