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. 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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int main()
{
int n;
int num; //数组存储方式调换,便于交换arr[0]和arr[arr[0]]
int arr[maxn] = {};
scanf("%d",&n); int left = n - ; //控制循环次数,需要交换数的个数,0除外
for (int i = ; i < n; i++)
{
scanf("%d",&num);
arr[num] = i;
if (num == i && num != )
{
left--;
}
} int k = ; //如果排序还未最终完成但是 0 已经回到arr[0]处,交换arr[k]值
int ans = ; //纪录交换的次数
while (left > )
{
if (arr[] == )
{
while (k < n)
{
if (arr[k] != k)
{
swap(arr[], arr[k]);
ans++;
break;
}
k++;
}
}
else
{
swap(arr[], arr[arr[]]);
ans++;
left--;
}
} printf("%d", ans);
return ;
}
10-排序6 Sort with Swap(0, i) (25 分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- A1067 Sort with Swap(0, i) (25 分)
一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
- PAT 1067. Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
随机推荐
- j2ee的容器:web容器和ejb容器的概念
在J2EE中,容器充当的是中间件的角色. 两种主要容器的概念 Web容器 给处于其中的应用程序组件(JSP.Servlet)提供一个环境,使得JSP,Servlet能直接和容器中的环境变量.接口交互而 ...
- 基于第三方vuejs库组件做适配性个性开发
相信大家在使用vuejs时候会用到很多的第三方库,能够找到适合自己的库并且加以使用可以大大加快进度,减少bug.但是很多时候会出现这样一个尴尬的境地: 基线的第三方组件并不能很好地满足我们自己地需求, ...
- javascript工厂函数(factory function)vs构造函数(constructor function)
如果你从其他语言转到javascript语言的开发,你会发现有很多让你晕掉的术语,其中工厂函数(factory function)和构造函数(constructor function)就是其中的一个. ...
- 活动任务出现bug
之前做的一个活动任务发现一个bug,是以前和离职同事一起对逻辑的时候没有考虑到的,配置活动的时候应该要先查询下,如果这个产品线上在这段时间已经配置了并且上线了,则不能在做活动处理了,否则就和前面的活动 ...
- Celery简介以及Django中使用celery
目录 Celery简介 消息中间件 任务执行单元 任务结果存储 使用场景 Celery的安装和配置 Celery执行异步任务 基本使用 延时任务 定时任务 异步处理Django任务 案例: Celer ...
- 遇到了Microsoft Visual Studio is Busy!
最近两天,我点击F5进入调试模式,VS左下角状态显示一直在加载符号文件,然后加载的超级慢,不多一会儿,显示VS正忙!如上图所示. 然后的然后,VS就卡死了~~~.之后,在网上查找原因和解决办法,找来找 ...
- 不懂APS系统?十个问答让你对APS瞬间明明白白
本文为您解答APS自动排程系统导入中客户常见的问题,帮助您评估企业是否适合导入APS,并了解需要的人力和资金的投入. Q1:哪些企业需要导入APS? A1: 编制生产计划有困难的企业都可以开始考虑导入 ...
- android studio学习----The project encoding (windows-1252) does not match the encoding specified in the Gradle build files (UTF-8)
Warning:The project encoding (windows-1252) does not match the encoding specified in the Gradle buil ...
- Django框架(五)-- 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传
一.视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...
- MySQL数据库(三)--表相关操作(二)之约束条件、关联关系、复制表
一.约束条件 1.何为约束 除了数据类型以外额外添加的约束 2.约束条件的作用 为了保证数据的合法性,完整性 3.主要的约束条件 NOT NULL # 标识该字段不能为空,默认NULL,可设置NOT ...