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 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,*)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 ...
- 1067. Sort with Swap(0,*) (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...
- 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 ...
- 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 ...
随机推荐
- ARC下的所有权修饰符
ARC有效时,id类型必须加上所有权修饰符 下面为三种等效的声明,为了便于和二级指针的情况联系起来,采用第一种. NSError * __weak error = nil; NSError __wea ...
- C#编写抽奖问题
输入每个人的中奖号码,进行滚动显示 //清屏 //随即一个索引 // 根据索引打印 //等待0.1秒 Console.Write("请输入参与者人数:&q ...
- 十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众
一:文起缘由 写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码. public void S ...
- mysql-3 检索数据(1)
SELECT 语句 SELECT检索表数据,必须至少给出两条信息--------想选择什么,以及从什么地方选择. 检索一个列 SELECT prod_name FROM products; 上述语句利 ...
- 烂泥:【解决】NFS服务器使用showmount –e命令报错
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天在NFS服务器使用showmount –e查看NFS共享目录时,发现系统一直显示如下错误: clnt_create: RPC: Port mappe ...
- iOS实现自定义进度条、拖动条效果,可多个
项目用到的一个场景,需要设置一个周期内不同时间时的数值 比如要设置10秒内,每一秒的大小,通过10个拖动条来设置实现,只需拖动到想要的数值即可, 这里周期10秒和每个拖动条的最大值都是可以自己定义的. ...
- glibc-2.15编译error: linker with -z relro support required
./configure --prefix=/usr/local/glibc-2.15 configure: error: you must configure in a separate build ...
- bg激活后台运行的服务
按redis重启来做案例 ./redis-server Ctrl+z让执行的命令在后台暂停 [1]+ Stopped ./redis-server 这个时候他是把这个服务放到后台了,可是ctrl+z是 ...
- quad 和 plane 区别是什么?
Quad就是两个三角形组成四边形,Plane会有很多三角形,哦也 貌似Quad拖上去后看不见,很薄的感觉
- javascript里面this机制的几个例子
javascript里面的this值会随着使用场景的不同二发生变化,但是总有一个原则,那就是this总指向当前调用函数的那个对象.以下我会举几个例子来说明这个问题.1.this本身总是指向当前的类的实 ...