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 Nnonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤105) 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
题意:输入一个序列,如果某个数不在该位置,比如1不在1号位,那么需要和0交换,直到整个序列都在数所对应的位置上,过程中只能用0交换,求最小交换次数
分析:贪心题,要次数最小,只要每次和0交换后到达所对应的位置,简单地说就是换一次就不用再换了。这里要考虑两种情况,第一种是0不在0号位,那么找到0在的位置,比如在3号位,那么和三号位对应的数交换;第二种是0在0号位,找到第一个不在本位上的数交换。
为了方便起见,数组用来存数的位置。如下所示:
| t | 4 | 1 | 2 | 0 | 3 |
| a[] | 3 | 1 | 2 | 4 | 0 |
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-26-10.19.41
* Description : A1067
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
},num=;
scanf("%d",&n);
;
;i<n;i++){
scanf("%d",&t);
a[t]=i;
) left--;
}
;
while(left){
int i;
]==){
for(;j<n;j++){
if(a[j]!=j){
swap(a[j],a[]);
num++;
break;
}
}
}
else{
swap(a[],a[a[]]);
num++;
left--;
}
}
cout<<num;
;
}
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 ...
- 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,*) (25)【贪心】——PAT (Advanced Level) Practise
题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...
随机推荐
- bootstrap-treeview 自定义实现双击事件
bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结 ...
- webpack 添加 jquery 插件
webpack.base.config.js 加入以下配置: , plugins: [ new webpack.ProvidePlugin({ jQuery: 'jquery', $: 'jquery ...
- Linux/Mac OS 个人常用Terminal技巧整理
刚开始接触linux有些不适应,走了不少弯路,一直没有系统的学过linux应用,基本都是零零散散Google出来的知识,在这里做个整理: Vi/Vim 基本操作: 刚开始接触linux时,不懂vi吃了 ...
- JAVA中static什么作用?
是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只 ...
- 【计算机视觉】交并比IOU概念理解
前言 交并比IOU(Intersection over Union)是一种测量在特定数据集中检测相应物体准确度的一个标准. 图示 很简单,IoU相当于两个区域重叠的部分除以两个区域的集合部分得出的结果 ...
- 获取APP图片资源
iOS开发项目-斗鱼直播APP - 网易云课堂 一. 二.导出Assets.car中的图片资源 cartool
- System.out.println()详解 和 HttpServletRequest 和 XMLHttpRequest
System是一个类,位于java.lang这个包里面.out是这个System类的一个PrintStream类型静态属性.println()是这个静态属性out所属类PrintStream的方法. ...
- Math类的学习 java 类库 API 文档学习
- MySQL Transaction--RC事务隔离级别下加锁测试
==============================================================================非索引列更新 在读提交的事务隔离级别下,在非 ...
- hive 数据导出三种方式
今天我们再谈谈Hive中的三种不同的数据导出方式.根据导出的地方不一样,将这些方式分为三种:(1).导出到本地文件系统:(2).导出到HDFS中:(3).导出到Hive的另一个表中.为了避免单纯的文字 ...