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. 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分)的更多相关文章
- 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 ...
- 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 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- 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 ...
- 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 ...
- A1067 Sort with Swap(0, i) (25 分)
一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 ...
- 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 ...
随机推荐
- RabbitMQ面试题集锦(精选)(另附思维导图)
1.使用RabbitMQ有什么好处? 1.解耦,系统A在代码中直接调用系统B和系统C的代码,如果将来D系统接入,系统A还需要修改代码,过于麻烦! 2.异步,将消息写入消息队列,非必要的业务逻辑以异步的 ...
- django数据库分库migrate
最近在研究微服务和分布式,设计到了数据库分库,记录一下 首先,创建多个数据库,如果是已经生成的数据库,可以分库,这里我是新创建的项目,刚好可以用来尝试 我是用docker创建的mysql数据库容器 拉 ...
- appnium适应之配置
一.session #获取包名和acctivename#这个工具在adk包里面aapt.exe dump badging E:\Wandoujia_851097_web_seo_baidu_binde ...
- vue练手项目——桌面时钟
用vue实现一个简单的网页桌面时钟,主要包括时钟显示.计时.暂停.重置等几个功能. 效果图如下,页面刚进来的时候是一个时钟,时钟上显示的时.分.秒为当前实际时间,点击计时器按钮后,页面变成一个计时器, ...
- 【opencv系列01】OpenCV4.X介绍与安装
一.介绍 OpenCV(开源计算机视觉库:http://opencv.org)是英特尔开源的一个跨平台计算机视觉的BSD许可库,包含数百种计算机视觉算法.OpenCV由Gary Bradsky于199 ...
- Spring MVC系列-(3) Bean的装配
3. 高级装配Bean 3.1 Bean的作用域 默认情况下,Spring中的bean都是以单例的形式存在的,无论注入多少次,每次注入的都是同一个实例. 考虑到某些bean可能是可变的,Spring定 ...
- 【docker Elasticsearch】Rest风格的分布式开源搜索和分析引擎Elasticsearch初体验
概述: Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎. 它能从项目一开始就赋予你的数据以搜索.分析和探索的能力,这是通常没有预料到的. 它存在还因为原始数据如果只是躺在磁 ...
- 第三周java实验报告
实验三 Java基本程序设计(2) 实验时间 2018-9-13 第一部分:理论知识回顾 第一章 再次了解了java“白皮书”的关键术语,java的常见术语,对于大多数“白皮书”的关键术语依然 ...
- CF1326A Bad Ugly Numbers 题解
原题链接 简要题意: 构造一个长为 \(n\) 的数,使得每位均不为 \(0\),且 \(n\) 不被它的各位数字整除. 比方说, \(n = 239\) 是合法的.因为: \(2 \not | 23 ...
- 李宏毅老师机器学习课程笔记_ML Lecture 1: ML Lecture 1: Regression - Demo
引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...