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. java.lang.IllegalStateException:Web app root system property already set to different value 错误原因及解决 Log4j

    Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口 服务器.NT的事件记录器.UNIX Syslog守护进程等: ...

  2. JAVA中获取路径

    内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...

  3. 安装使用ubuntu问题汇总

    很早以前就安装了ubuntu系统,可是一直没怎么用,也没有深入研究.这两天重装了一下windows,顺带着也重新装了一遍最新的ubuntu14.04.期间碰到了不少问题,一个个解决也花费了不少时间.所 ...

  4. java怎么建立JAVA工程项目?

    File->New->Java Project;src->New->Class; 出现packet,运行出错的问题 然后如果不要包packet 的话,不要在此处填写包的名称就行 ...

  5. Microsoft.Bcl.Build 1.0.10 稳定版发布

    Microsoft.Bcl.Build 1.0.10 稳定版发布 解决了之前 1.0.8 在未下载相应的Nuget Package 的情况下项目无法加载的情况 但由于 Microsoft.Net.Ht ...

  6. AC日记——基因相关性 openjudge 1.7 03

    03:基因相关性 总时间限制:  1000ms 内存限制:   65536kB 描述 为了获知基因序列在功能和结构上的相似性,经常需要将几条不同序列的DNA进行比对,以判断该比对的DNA是否具有相关性 ...

  7. web 小知识

    document.write和innerHTML的区别   document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关 ...

  8. MySQL 基础语句的练习

    -- *** ①.对数据库的操作 *** -- -- 查看所有的数据库 SHOW DATABASES; -- 创建数据库 CREATE DATABASE day1026 DEFAULT CHARACT ...

  9. poj[3093]Margaritas On River Walk

    Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

  10. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...