PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分)
题意
给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次。
思路
贪心
由于是排列,所以排序后第 \(i\) 个位置上的数就是 \(i\)。所以当 \(a[0] \neq 0\) 时,把 \(a[0]\) 位置上的元素交换到相应位置。如果 \(a[0] = 0\),就找到第一个不在正确位置上的数,把它与第 \(0\) 个数交换,那么下一次又是第一种情况了,也就是下一次交换可以换到正确的位置。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int arr[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int c = 0, cnt = 0;
while (c < n) {
if (arr[0] != 0) {
swap(arr[0], arr[arr[0]]);
cnt++;
} else {
for (; c < n && arr[c] == c; ++c);
if (c == n) break;
swap(arr[0], arr[c]);
cnt++;
}
}
cout << cnt << endl;
return 0;
}
PTA 1067 Sort with Swap(0, i) (贪心)的更多相关文章
- PTA 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 increasin ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
- 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,*) (25)【贪心】——PAT (Advanced Level) Practise
题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...
- 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 ...
- 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 ...
- PTA(Advanced Level)1067.Sort with Swap(0, i)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...
- 1067. Sort with Swap(0,*) (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...
随机推荐
- [BZOI 3994] [SDOI2015]约数个数和(莫比乌斯反演+数论分块)
[BZOI 3994] [SDOI2015]约数个数和 题面 设d(x)为x的约数个数,给定N.M,求\(\sum _{i=1}^n \sum_{i=1}^m d(i \times j)\) T组询问 ...
- python学习第四十二天列表生成式用法及作用
在操作列表或者元组的时候,对一系列的数据进行算法,比较整个数据加1,或翻倍,用传统的算法就很繁琐,列表给我们提供简便的方法 a=[i*i for i in rang(10)] a=[1,4,9,16, ...
- asp.net 获取表单中控件的值
原文:https://blog.csdn.net/happymagic/article/details/8480235 C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...
- 15、前端知识点--MVVM
MVVM模式的理解 MVVM模式里面,核心是数据. 各种前端框架,最核心的说就是保持了数据与视图的同步. 数据驱动思想:数据驱动视图. Vue不建议手动操作DOM. 以前是指令操作DOM,其实本质上底 ...
- 短篇文档兼职看过来 python 课后作业 assignment project
文档兼职 开题报告 读后感 课后作业 等 代写 编程,Java ,Python,R,等语言的,国内外课程作业指导,写作. 有经验,有作品,成交快,放心! 可联系 QQ 550987425
- vue.js(11)--案例--关键字搜索列表
关键字搜索品牌案例 (1)页面布局 <div class="app"> <div class="panel panel-primary"> ...
- VS2012 改C# 模版
原始文件位置: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache\CSharp\Co ...
- 关于Python获取图片文件二进制数据的问题(获取为空)
在搭建fastdfs文件系统的时候遇到了点问题,在测试上传文件数据流的时候,需要Python来获取本地文件的二进制流 from fdfs_client.client import Fdfs_clien ...
- mvn 打包排除test
mvn clean package compile -Dmaven.test.skip=true
- AIX下的ha高可用集群cluster
安装ha软件 一.安装软件 最稳定的版本是5.4.0,优先安装稳定版本5.4 安装依赖于包base.data, cluster的man包安装失败原因是缺少base.data包 安装所有cluster. ...