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. 这样学习Servlet,会事半功倍!!

    前言 工作已经有一段时间了,如果让我重新学Servlet,我会怎么学呢?下面抛出两个常见的问题,我分开来解答 2020年了,还需要学Servlet吗? Servlet的学习路线(学习重点) 一.202 ...

  2. JS实战(京东秒杀)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Java并发编程(02):线程核心机制,基础概念扩展

    本文源码:GitHub·点这里 || GitEE·点这里 一.线程基本机制 1.概念描述 并发编程的特点是:可以将程序划分为多个分离且独立运行的任务,通过线程来驱动这些独立的任务执行,从而提升整体的效 ...

  4. WINDOWS上JDK安装与环境变量设置

    一.JDK安装 jdk版本:jdk1.8.0_144 下载链接:https://pan.baidu.com/s/1eS2bFhg 密码:e3q1 下载JDK后点击安装,可以根据需要修改JDK的安装目录 ...

  5. vue中nextTick的使用场景

    https://blog.csdn.net/bingqise5193/article/details/100212278

  6. Clipboard.SetText()卡住问题

    调用 Clipboard.SetText(),每次都抛出异常:"CLIPBRD_E_CANT_OPEN" 调查后发现,实际上SetText有成功的将文本复制到Clipboard,但 ...

  7. Set-Get(?占位符)-Java(新手)

    创建实体类: package JdbcDome; public class EmpL { private int uid; private String uNAME; private int age; ...

  8. Java后台面试记录

    腾讯一面: 总结:考基础和代码(网址A是不是网址B的子域) + SQL(选出重复邮箱)(以下是没回答上来的) 逻辑回归公式(简历上写了协同过滤) 详见:https://blog.csdn.net/ma ...

  9. Redis 【常识与进阶】

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  10. ThinkPHP5.0 漏洞测试

    ThinkPHP5.0 漏洞测试 自从ThinkPHP发布漏洞补丁以来,服务器不知道多少次受到了批量扫描漏洞来抓取肉鸡的请求 虽然官方早已发布补丁,还是想试一下TP漏洞,测试两个漏洞 一.全版本执行漏 ...