7-16 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. 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 (≤10^5) 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

这道题用到了表排序中N个数字的排列由若干独立的环组成的知识。

1.单元环swap(0,i)次数为0;

2.有0的非单元环,swap(0,i)次数为n-1;

3.无0的单元环,可以先把环里随便一个数和0交换一次,这样整个环就变成了含0的n+1个元素的非单元环,根据前面的情况2,次数为(n+1)-1,但还要加上把0换进去的那次,故swap(0,i)次数为n+1;

代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int flag=0; // 记录circle是否含0;
int main(){
int N; cin>>N;
// 储存数列
vector<int> sequence; // 为了已知元素找下标而建立的position集合
// 其中pos[i]表示sequence中数值为i的元素的下标为pos[i]
vector<int> pos; sequence.resize(N); // 不能少,为sequence创造N个空间
pos.reserve(N); // 不能少 //读入数据和初始化pos
for(int i=0;i<N;i++) {
scanf("%d",&sequence[i]);
pos[sequence[i]]=i;
} //temp储存circle中第一个位置的元素
//swap_times记录swap(0,i)的次数
//elements_num记录circle中元素的个数
int temp,swap_times=0,elements_num=0;
for(int element=0;element<N;element++){ //temp0用来进行下面的赋值活动
int element_circle=element; int temp0;
temp=sequence[element_circle]; //每次进入circle前初始化elements_num为0;
elements_num=0;
//若为单元环,则不进入while,故elements_num为0; while(pos[element_circle]!=element_circle){//判断当前位置sequence[i]是否为i,即是否是单元环和环是否结束;若不是,则继续 if(sequence[element_circle]==0)
flag=1;//记录该circle是否有0
elements_num++; // 记录circle元素个数
if(pos[pos[element_circle]]!=pos[element_circle])//用于判断当前元素是否环的尾巴
sequence[element_circle]=element_circle; //不是尾巴
else
sequence[element_circle]=temp;// 是尾巴,用temp来赋值 //下面部分用来更新pos和移动element_circle
temp0=element_circle;
element_circle=pos[element_circle];
pos[temp0]=temp0;
//
}
if(elements_num!=0){ // 判断是否是单元环
if(flag==1) swap_times+=elements_num-1; //判断circle是否有0,有0的非单元环移动elements-1次
else swap_times+=elements_num+1; //无0的非单元环移动elements+1次
} //归零flag和elements_num
flag=elements_num=0;
}
cout<<swap_times<<end;
return 0;
}

7-16 Sort with Swap(0, i)(25 分)的更多相关文章

  1. PTA 10-排序6 Sort with Swap(0, i) (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

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

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

  6. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  7. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

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

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

随机推荐

  1. 【SCOI2016】Day1 模拟

    2018.8.16 8:00~11:06 先看t1,成功读错题... 以为是一个字符串的所有后缀都得在计划表里,否则权值就得是$n^2$ 然后花了一个小时多一点写了一个错误的做法 然后没有分 才发现看 ...

  2. Codeforces 1107E(区间dp)

    用solve(l, r, prefix)代表区间l开始r结束.带了prefix个前缀str[l](即l前面的串化简完压缩成prefix-1个str[l],加上str[l]共有prefix个)的最大值. ...

  3. linux系统文件目录解析

    /bin 二进制可执行命令  /dev 设备文件(硬盘/光驱等)  /etc 系统管理和配置文件  /etc/rc.d 启动的配置文件和脚本  /home 用户主目录,下面会有以登录用户名作为文件夹名 ...

  4. UGUI_屏幕适配

    引用:http://www.xuanyusong.com/archives/3278#comments 1.可以选择的有三种: 1.Screen Space – overlay  此模式不需要UI摄像 ...

  5. Vue 页面加载闪现代码问题

    CSS中 [v-cloak] { display: none; } HTML中 <div v-cloak> {{ message }} </div> 显示代码主要是{{}}这个 ...

  6. Entity Framework + MySQL 使用笔记

    添加: using (var edm = new NorthwindEntities()) { Customers c = ", Region = "天府广场", Con ...

  7. HYSBZ 1086 王室联邦 (树的分块)

    题意:国王想把他的国家划分成若干个省.他的国家有n个城市,是一棵树,即n-1条边,编号为1..n.为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市.每个省必须有 ...

  8. 迅为4412开发平台Zigbee模块在物联网智能家居中的应用

      物联网智能家居的发展物联网随着互联网的发展,可以通过互联网实现物和物的互联,就有了物联网的概念.传统家居电器 有了物联网之后,在家居电器范围中,就是通过物联网技术将家中的各种设备连接到一起,家居中 ...

  9. hdu5739Fantasia(多校第二场1006) 割点+逆元

    Fantasia Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  10. Boxes And Balls(三叉哈夫曼编码)

    题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...