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 ...
随机推荐
- 玩转X-CTR100 l STM32F4 l DS18B20单总线温度传感器
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 扩展DS1 ...
- bjui的validate表单验证的使用
date-rule ="date" 表示格式为yyyy-MM-dd date-rule = "datetime" 表示格式为yyyy-MM-dd HH:mm:s ...
- 在C#中如何定义一个变长的结构数组?如果定义好了,如何获得当前数组的长度?
用ArrayList,他就相当于动态数组,用add方法添加元素,remove删除元素,count计算长度
- vue|html5 form 验证
html:<form id="scoreForm" @submit="fsub" > <template v-for="(item, ...
- Redis学习第七课:键值命令和服务器命令
Redis键值相关命令 ...
- Spring JdbcTemplate中的回调
回调 JdbcTemplate类支持的回调类: 1.预编译语句及存储过程创建回调:用于根据JdbcTemplate提供的连接创建相应的语句: 1.1 PreparedStatementCreator ...
- Ubuntu 搭建Ghost1.0博客系统
最近想使用Ghost搭建自己的博客网站,网上搜索了下大多都是1.0之前版本搭建的文章,但是Ghost1.0版本已经可用好一段时间了,所以决定根据官方文档搭建Ghost1.0版本的博客系统. 下面开始一 ...
- NBUT 1224 Happiness Hotel 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB The life of Little A is good, and, he managed to get enoug ...
- Unity插件-ShareSDK使用指南
Unity插件ShareSDK使用教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Summariz ...
- 【codeforces div3】【E. Cyclic Components】
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...