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. asp.net mvc 之旅—— 第二站 窥探Controller下的各种Result

    平时我们在Action中编码的时候,我们都知道所有的Action返回值类型都是ActionResult,并且我们的返回值也是各种奇葩,比如:Json(),Content(), View()等等...当 ...

  2. Druid 数据库连接池监控配置(web项目)

    Spring数据源配置: <!-- 数据源 --> <!--<bean id="dataSource" class="org.apache.com ...

  3. W3School-CSS 文本实例

    CSS 文本实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CSS 内边距 (paddi ...

  4. mysql学习笔记(一)

    my建表操作 创建表 create Table <表名> ( 字段名1,数据类型 [列级约束] [默认值], 字段名2,数据类型 [列级约束] [默认值], ... [表级约束], [co ...

  5. 从客户端中检测到有潜在危险的Request.Form值的详细解决方案

    ASP.Net1.1后引入了对提交表单自动检查是否存在XSS(跨站脚本攻击)的能力.当用户试图用之类的输入影响页面返回结果的时候,ASP.Net的引擎会引发一个HttpRequestValidatio ...

  6. 烂泥:yum的使用及配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum是什么 2. repo文件是什么 3. r ...

  7. x01.Game.Main: 从零开始

    一切从零开始,一切皆有可能. 浅墨,90后,<逐梦之旅>深入浅出,堪比大师. 1.安装 DXSDK_June10.exe 或更新版本. 2.运行 vs2012,新建 VC Win32 空项 ...

  8. gcc编译参数-fPIC的一些问题

    gcc编译参数-fPIC的一些问题 (2012-07-26 15:41:08) 转载▼ 标签: linux compiler gcc -fpic it 分类: NSN_BspDriver ppc_85 ...

  9. java 重载、重写、构造函数详解

    方法重写 1.重写只能出现在继承关系之中.当一个类继承它的父类方法时,都有机会重写该父类的方法.一个特例是父类的方法被标识为final.重写的主要优点是能够定义某个子类型特有的行为. class An ...

  10. Objective-C if语句处理 BOOL值不为1和0的情况

    BOOL ,布尔值,在Objective-C ,BOOL类型被typedef为signed char(有符号的整型),YES被#define为1,NO被#define为0. 事实上,xcode的编译器 ...