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 ...
随机推荐
- C/C++二维数组名和二级指针
转载 :https://blog.csdn.net/wu_nan_nan/article/details/51741030 作者:吴一奇 1. 指针1.1 一个指针包含两方面:a) 地址值:b) 所 ...
- Acwing.835. Trie字符串统计(模板)
维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...
- IDEA中Java目录结构
IDEA中Java的目录结构 1.首先新建Project,选择Empty,新建空的项目 2.选择Module时候,需要选择JDK,JDK只需要选择到Java Home目录就可以了 3.创建好Modul ...
- 经典的最大流题POJ1273(网络流裸题)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- 如何在Set集合中避免重复元素
文章翻译自 Avoiding near-duplicates in sets, 作者Paul Hudson @twostraws是一名优秀的Swifter. 这是我第一次翻译,可能有翻译不到位的地方, ...
- 自定义、操作cookie
/** * 读取所有cookie * 注意二.从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交.浏览器提交Cookie时只会提交name与value属性.maxAg ...
- openstack stein部署手册 8. neutron-api
# 建立数据库用户及权限 create database neutron; grant all privileges on neutron.* to neutron@'localhost' ident ...
- hadoop HA架构
什么是Hadoop? http://hadoop.apache.org/ 解决问题:·海量数据的存储 (HDFS)·海量数据的分析 (MapReduce)·资源管理调度 (YARN) 集群规划:(这里 ...
- APKMirror - 直接下载google play里的应用
APKMirror - Free APK Downloads - Download Free Android APKs #APKPLZ https://www.apkmirror.com/
- [BZOJ] 最长距离
问题描述 windy 有一块矩形土地,被分为 NM 块 11 的小格子. 有的格子含有障碍物.如果从格子 A 可以走到格子 B,那么两个格子的距离就为两个格子中心的欧几里德距离.如果从格子 A 不可以 ...