题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/678

5-16 Sort with Swap(0, i)   (25分)

Given any permutation of the numbers {0, 1, 2,..., N-1N−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 NN nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive NN (\le 10^5≤10​5​​) followed by a permutation sequence of {0, 1, ..., N-1N−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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-07 11:35 答案正确 25 5-16 g++ 24 2
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 15/15 2 1
测试点2 答案正确 3/3 24 2
测试点3 答案正确 3/3 12 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 1/1 2 1
测试点6 答案正确 1/1 2 1 有点技巧性的题目,mooc上讲了,主要是找排序的环。有0环的交换次数为n-1,无零环为n+1
建了三个数组,A是存数,B是存i号元素放在了哪个位置,Checked是放这个元素有没有被访问过。 坑:必须加一个全局变量做搜索函数起点的备忘录用,否则搜索函数反复调用会导致超时
*/
#include<stdio.h>
#define MAXN 100000
int A[MAXN],B[MAXN],Checked[MAXN];
int gFindBeginPosition=0;
int FindUnchecked(int N)
{
int i;
for(i=gFindBeginPosition;i<N;i++)
{
if(!Checked[i])
return gFindBeginPosition=i;
}
return -1;
} int main()
{
int i,N,p,sum;
int ringCount=0;
int nCount=0;
int zeroInPosition=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
B[A[i]]=i;
}
if(A[0]==0)
zeroInPosition=0;
else
zeroInPosition=-2; while((p=FindUnchecked(N))+1)
{
Checked[p]=1;
if(A[p]!=p)
nCount++;
else continue;
while(!Checked[B[p]])
{
p=B[p];
Checked[p]=1;
nCount++; }
ringCount++;
} sum=nCount+ringCount+zeroInPosition;
printf("%d",sum);
}

  

PTA 10-排序6 Sort with Swap(0, i) (25分)的更多相关文章

  1. PAT 甲级 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 ea ...

  2. 10-排序6 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  3. 1067 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  4. 1067 Sort with Swap(0, i) (25分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  5. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  6. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

  7. PTA(Advanced Level)1067.Sort with Swap(0, i)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

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

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

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

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

随机推荐

  1. 【Troubleshooting Case】Exchange Server 组件状态应用排错?

    在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...

  2. web端 复合控件 响应回发

    AutoPostback="true";   自动提交 RdiobuttonList 属性→设计→编辑项→{ Enabled   是否可用 selected 是否选中 Text   ...

  3. pc端常见布局---水平居中布局 单元素不定宽度

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. ZOJ 3471 Most Powerful (状压DP,经典)

    题意: 有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子.已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量? 思路: 一开始以为是找一个有序序列,使得能量最大 ...

  5. 51nod 1631 小鲨鱼在51nod小学

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务.   ...

  6. jQuery UI -- Repeater & 手风琴(Accordion)效果

    jQuery UI -- Repeater & 手风琴(Accordion)效果 很简单的范例,完全不用写程序 直接套用就能做! 但是,基础不稳的人,连「复制贴上」直接套用, 对您而言,都困难 ...

  7. Spring boot 集成Kafka

    搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...

  8. SC || Chapter6 复习向 面向可维护性 我哭了

    高内聚低耦合 高内聚:一个模块内部各个元素彼此结合的紧密程度,一个软件模块是由相关性很强的代码组成,只负责一项任务,也就是常说的单一责任原则 低耦合:各模块间相互联系紧密程度,模块间接口的复杂性.调用 ...

  9. python之函数的传参形参的第三种动态参数*args和**kwargs

    1. 位置/关键字传参的缺点 当给函数传入的参数数目不定时,之前的传参方式解决不了问题. def eat(food1,food2,food3): print(f'我请你吃:{food1},{food2 ...

  10. c#中的里氏转换和Java中强制类型转换在多态中的应用

    在c#中: 注意: 子类并没有继承父类的构造函数,而是会默认调用父类那个无参数的构造函数. 如果一个子类继承了一个父类,那么这个子类除了可以使用自己的成员外,还可以使用从父类那里继承过来的成员.但是父 ...