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 ...
随机推荐
- [2019杭电多校第三场][hdu6608]Fansblog
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6608 大致题意是比p小的最大素数q,求q!%p的值. 由威尔逊定理开始推: $(p-1)!\equiv ...
- QToolButton设置icon的大小
项目中用到了QToolButton上使用图片. 如果在maindow中直接使用QToolButton,如: btnSimulate = new QToolButton; btnSimulate-> ...
- Html5移动端页面自适应布局详解(rem布局)
在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport,通读网上的各种对于viewport的解释之后 大概viewport可以理解为三种 1.layout viewport ...
- Webstorm上已有的本地项目上传到Github
1.Webstorm左上角File —— Settings—— Github, Auth Type改成password,在Login和Password中输入自己的github账户和密码,点Test测试 ...
- javascript中无法将string转化为json对象
在一次项目之中,我要对请求的相应做一些处理,得到的响应差不多是这中格式'{total:1,result:[{"age":1}]}'.可以看到我拿到的这个相应和JSON的格式是非常相 ...
- jvm监控和诊断工具
大牛写的Java的OOM Killer:https://www.jianshu.com/p/4645254be259 强烈推荐 总的参考链接:https://cloud.tencent.com/dev ...
- 一、C++类库与C#类库相互调用
1.C++调用C#类库 1.准备C#类库(dll文件) 1.1.创建C#类库: 右击项目类库生成即可, 出现.dll(类库)与.pdb(pdb文件包含了编译后程序指向源代码的位置信息, 用于调试的时候 ...
- 零点.Net Core 接触
一.Program.cs类与Startup类 1.一切从Main开始,Main方法包含了是整个应用程序的入口 ASP.NET Core应用程序可以配置和启动主机(Host). 主机负责应用程序启动和生 ...
- bzoj2306 [Ctsc2011]幸福路径 倍增 Floyd
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2306 题解 倍增 Floyd. 令 \(f[i][j][k]\) 表示走了 \(2^i\) 步 ...
- django之类视图
一:类视图 1. 为什么使用类视图? # 以注册请求逻辑为例 def register(request): if request.method == "GET": render(r ...