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 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
解题思路
首位为0,找出第一个未排序的数的位置进行交换
首位非0。与该数应处的位置交换
AC代码
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a, b;
int main()
{
int n, t, c = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", &t);
a.push_back(t);
}
int cnt = 0;
while (c < n){ //序列排序未结束
if (0 == a[0]){ //首位为0,找出第一个未排序的数的位置
for (; c < n && c == a[c]; ++c)
continue;
if (c >= n) break;
swap(a[0], a[c]);
++cnt;
}else{ //首位非0。与应处于的位置交换
swap(a[0], a[a[0]]);
++cnt;
}
}
printf("%d\n", cnt);
return 0;
}
个人游戏推广:
《10云方》与方块来次消除大战!
1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise的更多相关文章
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 ...
- PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...
- 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 ...
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 分)
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...
随机推荐
- 多线程编程TSL相关的技术文档
线程本地存储 (TLS) https://msdn.microsoft.com/zh-cn/library/6yh4a9k1(v=vs.80).aspx Using Thread Local Stor ...
- c7---函数
// // main.c // 函数练习 // // Created by xiaomage on 15/6/7. // Copyright (c) 2015年 xiaomage. All right ...
- maven冲突管理及依赖管理实践
1.“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突 Maven采用“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突,即如果一个项目最终 ...
- USACO 1.4 Arithmetic Progressions
Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...
- DB-MySQL:MySQL 正则表达式
ylbtech-DB-MySQL:MySQL 正则表达式 1.返回顶部 1. MySQL 正则表达式 在前面的章节我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配. MySQL 同 ...
- Upload图片-单张
上传图片全不怕,轻松实现图片上传, 可以实现显示缩略图喔: 后台代码: protected void btnpic_upload_Click(object sender, EventArgs e) { ...
- jsp指令和学习笔记集锦
Jsp包含三个编译指令和七个动作指令.三个编译指令为:page.include.taglib. 七个动作指令为:jsp:forward.jsp:param.jsp:include.jsp:plugin ...
- 激情世界杯,盛夏大放价,CDR 618返场继续嗨
最近被刷屏应该就是世界杯.世界杯和世界杯了... 进行了到第七天的球迷们,你们还好么 私房钱还剩下多少?上班有没有请假迟到? 哎,中国的小龙虾都去俄罗斯了,就国足队员没去… 满屏而来的不仅是手机朋友圈 ...
- my.cnf配置样例
[mysql] no-auto-rehash port = socket = /data/mysql/data/mysqld.sock [mysqld] user = mysql port = bas ...
- Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题
不解释,题目过水 Code: #include<cstdio> #include<cmath> #include<algorithm> using namespac ...