题目地址

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. Windows计算机重置TCP / IP

    传输控制协议 (TCP / IP)是Internet上使用的通信协议. 在Windows的早期版本中,TCP / IP是一个单独的可选组件,可以像其他任何协议一样删除或添加. 早期版本中,从Windo ...

  2. Python+Selenium之摘取网页上全部邮箱

    本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...

  3. URAL 1057 Amount of Degrees (数位DP,入门)

    题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+2 ...

  4. 字符编码ANSI和ASCII区别、Unicode和UTF-8区别

    ANSI码ANSI编码是一种对ASCII码的拓展:ANSI编码用0x00~0x7f (即十进制下的0到127)范围的1 个字节来表示 1 个英文字符,超出一个字节的 0x80~0xFFFF 范围来表示 ...

  5. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

  6. mysql中的空值问题

    MySQL的查询如果需要用到空值的情况下,where后面的条件就需要注意了 MySQL中的表示空值的方法:is null 和 is not null 比如:select * from user whe ...

  7. C++内存溢出和内存泄漏?

    1.内存溢出 内存溢出是指程序在申请内存时没有足够的内存空间供其使用.原因可能如下: (1)内存中加载的数据过于庞大: (2)代码中存在死循环: (3)递归调用太深,导致堆栈溢出等: (4)内存泄漏最 ...

  8. python-下拉框处理

    在自动化中python对下拉框的处理网上相对实例比较少,其它前辈写的教程中对下拉也仅仅是相对与教程来说的,比如下面: m=driver.find_element_by_id("Shippin ...

  9. 用C#(ASP.Net)在Exchange Server环境下发送邮件

    普通的邮件, 用System.Net.Mail 类 或 System.Web.Mail 类 处理即可, 但是Exchange Server 环境下, 这两个类起不了作用-------至少目前我看到的情 ...

  10. CE软件修改器

    下载地址: 链接:https://pan.baidu.com/s/1WQa5epfmLW92xk0XY10pqw 提取码:jt3k 喜欢请点赞