1067. Sort with Swap(0,*) (25)

 

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 (<=105) 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

一开始按照选择排序做了, 结果有两个测试点超时, 看看网上的思路基本上都是判断是否被访问的算法,想了想这个应该是表排序的变形,就按照表排序方式做了,花了一下午调试 果然AC了, 特地来交流 ^_^!

 #include <stdio.h>
#include <stdlib.h> typedef int ElementType; void Swap(ElementType *a, ElementType *b);
void PrintA(ElementType A[], int N);
int IsSort( ElementType A[], int m, int N);
void SwapZero( ElementType A[], ElementType B[], int N); typedef struct {
int index;//0元素所在下标
int count;//记录交换次数
}Zero; Zero zero; int main(){
int N, i;
zero.count = ;
//freopen("C:\\in.txt","r", stdin);
scanf("%d", &N);
ElementType* A;//这个是原来的数组
A = (ElementType*)malloc(N*sizeof(ElementType));
ElementType* B;//这个是表
B = (ElementType*)malloc(N*sizeof(ElementType));
for( i=; i<N; i++){
scanf("%d", &A[i]);
B[A[i]] = i;//记录A[i]所在的位置
if(A[i] == )
zero.index = i;
}
SwapZero(A, B, N);
printf("%d\n", zero.count);
return ;
} int IsSort( ElementType A[], int m, int N){
int i;
int flag = ;
for(; m<N; m++ ){
if( m != A[m] ) {
flag = m;
break;
}
}
return flag;
} void SwapZero( ElementType A[], ElementType B[], int N){
int i, m = ;
for( ; ; ){
if( zero.index != ){//交换swap(0,i);
Swap( &A[zero.index], &A[B[zero.index]]);
B[] = B[zero.index];
B[A[zero.index]] = zero.index;//更新表
zero.index = B[];
zero.count++;
} else if( m=IsSort(A, m, N)){ //找到第一个位置不对的数字交换
Swap( &A[zero.index], &A[m]);//交换,更新表
B[zero.index] = m;
B[A[zero.index]] = ;
zero.index = B[zero.index];
zero.count++;
} else break;
}
} void Swap(ElementType *a, ElementType *b){
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}

PAT 1067. Sort with Swap(0,*)的更多相关文章

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

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

  3. PAT 1067 Sort with Swap[难]

    1067 Sort with Swap(0,*) (25)(25 分) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is e ...

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

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

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

  6. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

  7. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

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

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

随机推荐

  1. [PL/SQL]使用PL/SQL实现ORACLE的Excel格式导入导出

    注:教程所使用的PL/SQL Developer版本为10版本 1.oracle导出excel格式 第一步,在pl/sql窗口查询出你想要导出的数据. 第二步,选择你想导出的数据,然后右键" ...

  2. 玉渊潭赏樱花有感:从无到有写一个jQuery开源插件

    “玉渊潭公园樱花节”是每年樱花绽放时,都会在玉渊潭公园樱举办樱花节,游客前往玉渊潭公园,可以欣赏到20个品种2000株樱花.2016玉渊潭樱花节时间:3月中旬-4月中旬观赏最佳,2016年3月23日开 ...

  3. SQL指令中一些特别值得注意的地方

    SQL基本指令要频繁使用,要是理解错了,将来工作一定会出现很大的麻烦.今天再重新梳理一下基本的SQL语法 SQL指令 in: 这个指令,我曾经发生过把它和python中的in搞混的错误.python中 ...

  4. Java的SPI机制与简单的示例

    一.SPI机制 这里先说下SPI的一个概念,SPI英文为Service Provider Interface单从字面可以理解为Service提供者接口,正如从SPI的名字去理解SPI就是Service ...

  5. ctargs使用

    ctargs为源码的变量/对象.结构体/类.函数/接口.宏等产生索引文件,以便快速定位.目前支持41种语言,这里仅以C/C++为例:ctags可以产生c/c++语言所有类型的索引文件,具体如下: -& ...

  6. emacs 新手笔记(四) —— 使用 dired 完成一些简单的文件和目录操作

    ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 据说熟悉了 dired,就可以在 emacs 下轻松管理文件和目录了,甚至不再需要 shell 和桌面操作. ...

  7. BZOJ1216[HNOI2003]操作系统 [模拟 优选队列]

    1216: [HNOI2003]操作系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 754  Solved: 421[Submit][Status ...

  8. jmeter 与 java http

    jmeter 如果对java代码进行测试 1.eclips中创建一个项目,且写一个待测试的简单java代码 2.将jmeter路径下 x:\xx\lxx\Dowxxxxxx\apache-jmeter ...

  9. [多图]Windows 10 Build 10565今推送:优化界面菜单 Cortana改进

    酷站网软:此前的Windows Build 10558并没有向公众发布,而是直到近日才向Fast Ring用户推送了更多功能和改进的Build 10565版.除之前版本上的加入了Messaging.E ...

  10. 解决为什么每次打开Eclipse新的workspace需要更新nexus-maven-repository-index问题

    解决为什么每次打开Eclipse新的workspace需要更新nexus-maven-repository-index问题 新建一个Eclipse的workspace. 打开Window—>Pr ...