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 ...
随机推荐
- MySQL5.7 数据库的my.cnf配置
[client] port socket=/tmp/mysql.sock default-character-set=utf8 [mysql] no-auto-rehash default-chara ...
- 基于 HTML + WebGL 结合 23D 的疫情地图实时大屏 PC 版
前言 2019年12月以来,湖北省武汉市陆续发现了多例肺炎病例,现已证实为一种新型冠状病毒感染引起的急性呼吸道传染病并蔓延全国,肺炎疫情牵动人心,人们每天起来第一件事变成了关注疫情进展,期望这场天灾早 ...
- 原生js实现随着滚动条滚动,导航会自动切换的效果
最近学习前端,把前面用原生js写的一段有关tab切换效果的代码贴上,实现的效果比较简陋,请大家见谅 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- plantUML最佳实践
plantUML 使用plantUML中的活动图用来画流程图很好用; 但类图等就不很好用; 个人体会如下: • 时序图 推荐 • 用例图 一般 • 类 图 不推荐, 用Visual Paradigm或 ...
- 微信小程序开发(一)开发工具推荐VSCode
虽然微信小程序官方开发工具非常优秀,但用的时间久了,会发现一些问题,比如代码编辑区小,自定义能力差,不支持插件,有时还会出现莫名其妙的bug,最不能忍的是编辑器代码提示功能不健全,这对于新手来说,很不 ...
- 数据库表结构查询SQL
今天给大家送上两个SQL查询的方法,也许在项目中你都用过,但是,没关系,仅仅记录下,以便后面不用去查找.针对与经常写SQL的同行来说,应该是非常简单的. 查询表结构数据 SELECT t.COLUMN ...
- Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan (解读人:王茹凯)
文献名:Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan(平分GlcNAc是N-聚糖末端修饰的 ...
- 安装SQL Server 2008R2 报错“此计算机上安装了 Microsoft Visual Studio 2008 的早期版本”解决方法
安装SQL Server 2008 R2报错“此计算机上安装了 Microsoft Visual Studio 2008 的早期版本,请在安装 SQL Server 2008 前将 VS2008 升级 ...
- 简单BBS项目开始(一)
1.BBS需求分析和创建ORM 1. 需要哪些表 1. UserInfo 1. username 2. password 3. avatar #头像图片 2. 文章表: title publish_d ...
- 常用正则表达式(手机号、邮箱、URL地址、身份证等等)
一.前言 不好的习惯:1.每一次用到正则都是上网copy一份,也没有去学习思考,看看都是什么意思: 2.一个项目里不同的地方用到了相同的校验,一直在重复的copy代码,并没有统一起来,万一哪天要修改规 ...