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 (≤) 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

题目分析:看浙大《数据结构》的时候见到过这题 重做并没有做成功 运行超时了 我想的是每次通过0与0的位置来归位 若0在这个过程中不小心被交换到了0的位置 那就得将0交换到还未被归为的那个元素上 思路是对的 做法导致时间复杂度过大
通过上面的分析 可以将0所在的看成一个环 只需要记录环中有多少元素 以及有多少环 当0所在的环计算完成后 就到另一个环去 过了3个测试点
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Address[];
int Array[];
void swap(int i, int j)//交换2个地址上的值 i,j为地址
{
Address[Array[i]] = j;
Address[Array[j]] = i;
int temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
}
int main()
{
int N;
int times = ;
cin >> N;
for (int i = ; i < N; i++)
{
cin >> Array[i];
Address[Array[i]] = i;
}
int flag = ;
while (flag)
{
if (Address[] == ){
for (int i = ; i < N; i++)
if (Address[i] != i) {
swap(Address[], Address[i]);
flag = ;
times++;
break;
}
else flag = ;
}
else{
swap(Address[], Address[Address[]]);
times++;
}
}
cout << times;
}

全过

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> int A[] = { };
int Position[] = { };
int IsRight[] = { };
void Swap(int i, int j)
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
int SwapTimes=;
int FindElements(int Pos)
{
int num=;
while (Position[Pos]!=Pos)
{
num++;
IsRight[Position[Pos]] = ;
Position[Pos] = Position[Position[Pos]];
}
return num; //返回元素的个数
}
void Charge(int N)
{
int num;
//从零开始计算
SwapTimes += FindElements()-; //交换次数比元素个数少一
for (int i = ; i < N; i++)
{
if (!IsRight[i])
SwapTimes += FindElements(i)+; //虽然交换次数比元素个数少一 但是要利用0来进行交换 所以时 这个环的元素加一
} //而把0元素添加到 环中先进行一次交换 所以 最后结果为 元素+1-1+1
}
int main()
{
int N;
scanf("%d", &N);
for (int i = ; i < N; i++)
{
int num;
scanf("%d", &num);
A[i] = num;
Position[num] = i;
if (A[i] == i)
IsRight[i] = ;
}
Charge(N);
printf("%d", SwapTimes);
return ;
}

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

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

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

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

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

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

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

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

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

  7. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

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

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

随机推荐

  1. javascript入门进阶(一)

    javascript 入门进阶 这里主要讲解一下在入门阶段很难注意的一些知识点,不一定有用.但是会了总比不会强. 1.HTML为<script>标签准备的6个属性: -async:可选.表 ...

  2. jenkins操作

    jenkins忘记用户名以及登录密码的解决方法 1.jenkins 根目录下找到config.xml,修改配置  <useSecurity>true</useSecurity> ...

  3. 基于Linux Lite 4.8制作“Windows 12 Lite”正式发布

    值得注意的是,这款Windows 12 Lite的宣传标语将矛头直指Windows 10,声称不会迫使用户更新.不会存在病毒或恶意软件.启动速度比Win 10快3倍.提供8万多款优质软件.允许与Win ...

  4. c++第一周测验

    本次得分为:14.00/14.00, 本次测试的提交时间为:2020-03-08, 如果你认为本次测试成绩不理想,你可以选择再做一次. 1 单选(1分) 下面程序片段哪个没错? 得分/总分 A. in ...

  5. django 用户认证 user对象

    django中的用户模型 内部带有很多的属性方法,我们可以直接使用 1 is_staff Boolean.决定用户是否可以访问admin管理界面.默认False. 2 is_active Boolea ...

  6. 前端基础面试题(js部分)

      前端基础面试题(JS部分)   1.几种基本数据类型?复杂数据类型?值类型和引用数据类型?堆栈数据结构? 基本数据类型:Undefined.Null.Boolean.Number.String值类 ...

  7. vuex 业务使用

    1 创建变量 cityVuex.js export default { state: { cityArr: [] }, mutations: { setCityArr (state, arr) { s ...

  8. 用Python算带有进度条的圆周率

    import time scale=50 print("执行开始".center(scale//2,"-")) start=time.perf_counter( ...

  9. weblogic漏洞练习

    About WebLogic WebLogic是美商Oracle的主要产品之一,系购并得来.是商业市场上主要的Java(J2EE)应用服务器软件(application server)之一,是世界上第 ...

  10. 图解Java设计模式之模板模式

    图解Java设计模式之模板模式 豆浆制作问题 模板方法模式基本介绍 模板方法模式原理类图 模板方法模式解决豆浆制作问题 模板方法模式的钩子方法 模板方法模式在Spring框架中的源码分析 模板方法模式 ...