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. _bzoj1191 [HNOI2006]超级英雄Hero【构图 并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1191 以锦囊作为节点,问题作为边“一步一步”构图,当一个时刻,某个联通块中边数>点数, ...

  2. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Highway

    Highway Accepted : 122   Submit : 393 Time Limit : 4000 MS   Memory Limit : 65536 KB Highway In ICPC ...

  3. Solr打分排序规则自定义【转】

    在搭建好solrCloud搜索集群后,通过编写基本的查询显示语句已经能够通过输入关键字查询到相应结果进行显示,但是在显示结果排序上以及不相关信息过滤问题上,如何制定合理的打分规则得到理想的结果集确实比 ...

  4. [转]Android 如何监听返回键,弹出一个退出对话框

    本文转自:http://blog.csdn.net/sunnyfans/article/details/8094349 Android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用 ...

  5. c++ 如何对拍

    首先要写好两个要对拍程序(假设是A,B),和一个制造数据的程序(设为made)   (要放在同一文件夹内) 编译得到A.exe , B.exe ,  made.exe 写一个对拍器 格式如下 @ech ...

  6. 在input标签里只能输入数字

    <input type='text' onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,''); ...

  7. 数据库SQL server 删除一张表中的重复记录

    --建立一张表 create table cat( catId int, catName varchar(40) ) --将下边的插入语句,多执行几次. insert into catvalues(1 ...

  8. 洛谷P1724 东风谷早苗

    题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的 ...

  9. 51nod 1417 天堂里的游戏

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 多年后,每当Noder看到吉普赛人,就会想起那个遥远的下午. Noder躺在草地上漫无目的的张望,二 ...

  10. 你的项目刚刚启动?是时候考虑Globalization了!

    今天继续由SAP成都研究院非典型程序猿, 菜园子小哥王聪给大家带来分享. 关于这个很长的定语的由来,请参考这篇文章,里面有王聪的背景介绍,包括他种菜的特长:当我用UI5诊断工具时我用些什么. 秋天到了 ...