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 ...
随机推荐
- day05——字典
day05 字典 字典-- dict: 字典无序的,可变的数据类型 用于存储大量数据,字典要比列表快,将数据和数据之间进行关联 lst1 = ['常鑫','李文虎','张宇'] # 姓名 lst2 = ...
- 【05】Jenkins:用户权限管理
写在前面的话 在一个企业研发部门内部,可能存在多个运维人员,而这些运维人员往往负责不同的项目,但是有可能他们用的又是同一个 Jenkins 的不同用户.那么我们就希望实现一个需求,能够不同的用户登录 ...
- pyhanlp的安装
github 的官方地址:https://github.com/hankcs/pyhanlp conda install -c conda-forge jpype1 pip install pyhan ...
- iOS11里判断Safari浏览器是无痕模式还是正常模式?
var isPrivate = false; try { window.openDatabase(null, null, null, null); } catch (_) { isPrivate = ...
- Spring Boot 使用 JWT 进行身份和权限验证
上周写了一个 适合初学者入门 Spring Security With JWT 的 Demo,这篇文章主要是对代码中涉及到的比较重要的知识点的说明. 适合初学者入门 Spring Security W ...
- eclipse 标记任务
eclipse 标记任务 eclipse Task Tags: TODO -用来提醒该标识处的代码有待返回继续编写.更新或者添加.该标签通常在注释块的源文件顶部. FIXME -该标签用来提醒你代码中 ...
- Objective-C的self.用法
转自:http://mobile.51cto.com/iphone-313096.htm 进入正题, 我们经常会在官方文档里看到这样的代码: MyClass.h @interface MyClass ...
- APS助众生药业突破运营管理瓶颈
众生药业一直致力于为了世界提供世界级的产品及服务,成立以来公司先后实施了ERP系统,CRM系统,WMS系统,OA系统,精益生产,朝着行业信息化水平领先的目标迈进. 但近年随着业务量的不断扩大,仅仅拥有 ...
- Odoo视图的共有标签
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826405.html 1)name (必选) 通过name值,查找标签 2)model 与view相关联的 ...
- 全局的 axios 默认值
(1)全局的 axios 默认值 axios.defaults.baseURL = 'https://api.example.com';/*默认请求路径*/ axios.defaults.header ...