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 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 (≤) 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
题目大意:给定n个数字(1到n),且每次只能跟0交换,问换成递增的最少步数。
思路:
核心是让每次交换尽量都把0和当前0这个位置的数组下标,这两个数换位置,这样就不浪费这一次交换。
记录下每个元素所在的位置存入数组pos,即pos数组存放的是pos[number]=ard,表示Number这个数字当前在ard这个位置
如果0不在第0个位置,那么每次可以交换0位置和0所在位置在递增之后应该有的元素。
比如说4 0 3 1 2。先把0和pos[0],即0和1交换,这样1就在它本来应该在的位置。数组变成了4 1 3 0 2,再交换,直到0回到了下标0这个位置。
这样一趟下来保证了每次操作都是有意义的,即每次交换都能把一个元素放回它应该在的位置。
如果这个元素这样交换下来,还没在它应该在的位置,即pos[number]!=number,那就让它去0呆着,下次交换会把它放回应该属于它的位置的。
参考博客:点我
代码:
#include<bits/stdc++.h>
using namespace std;
int pos[];
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i < n ; i ++){
int x;
scanf("%d",&x);
pos[x] = i;
}
int cnt = ;
for(int i = ; i < n ; i ++){
if(pos[i] != i){
while(pos[] != ){
swap(pos[],pos[pos[]]);cnt++;
}//如果0不在数组下标为0这个位置,那可以不停的交换0所在位置数组下标 和 0这两个元素。
if(pos[i] != i){
swap(pos[],pos[i]);
cnt++;
}
}
}
printf("%d\n",cnt);
}
PTA 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 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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回到自 ...
- 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 ...
随机推荐
- 学习笔记TF046:TensoFlow开发环境,Mac、Ubuntu/Linux、Windows,CPU版本、GPU版本
下载TensorFlow https://github.com/tensorflow/tensorflow/tree/v1.1.0 .Tags选择版本,下载解压. pip安装.pip,Python包管 ...
- Inotify机制的简单应用
编程之路刚刚开始,错误难免,希望大家能够指出. 一.Inotify机制 1.简单介绍inotify:Inotify可用于检测单个文件,也可以检测整个目录.当检测的对象是一个目录的时候,目录本身和目录里 ...
- 将文件夹下的所有csv文件存入数据库
# 股票的多因子分层回测代码实现 import os import pymysql # import datetime, time # from config import * database_ta ...
- 缓存与数据库一致性之三:缓存穿透、缓存雪崩、key重建方案
一.缓存穿透预防及优化 缓存穿透是指查询一个根本不存在的数据,缓存层和存储层都不会命中,但是出于容错的考虑,如果从存储层查不到数据则不写入缓存层,如图 11-3 所示整个过程分为如下 3 步: 缓存层 ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- 支付宝 生活号 获取 userId 和 生活号支付
第一:申请生活号. 第二:激活开发者 模式 ,并且上创 自己的 公钥 ( 支付宝 demo 里面有 ) 第三: 配置 回调地址 ( 用于前端 调用获取 auth_code 的时候 填写的回调地址,支 ...
- HTTP各种特性
一.Http客户端 1.浏览器.打开百度首页 2.Curl工具 二.CORS 跨域(浏览器的功能) 1.修改Server.js const http = require('http'); const ...
- Centos6.8通过yum安装mysql5.7 centos7.5适用
1.安装mysql的yum源 a.下载配置mysql的yum源的rpm包 根据上面3张图片中的操作下载下来的rpm文件可以通过如下命令获取: wget https://dev.mysql.com/ge ...
- Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端
WebService服务端项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...
- 【OpenStack】相关概念
网络 network和subnet Service subnets: 创建network,subnet, instances 官方示例 Network components: Switches/ Ro ...