Pat1067:Sort with Swap(0,*)
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 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 (<=105) 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 思路
贪心 + 桶排序。 1.用数组pos模拟N个桶记录输入的数的位置,数组的下标表示这个数。输入数字时根据输入次序i和数字大小num是否相等来决定需要和0交换的数字个数NumCount。
2.只要0没有在0位置,就将pos[0]与pos[pos[0]]交换.循环直到pos[0] == 0。
3.如果pos[0] == 0且还有数字不在正确的位置(NumCount > 0),那么就将0和最近的一个不在正确位置的数字交换。注意索引交换的数字时用index记录下标(不然有几个测试用例会超时),这样下一次pos[0] == 0需要索引新的交换数字时不用再重头开始。
4.用一个变量swaptimes统计交换次数,然后输出就行。 代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
int index = 1,NumCount = 0,swaptimes = 0;
vector<int> pos(N);
for(int i = 0;i < N;i++)
{
int num;
cin >> num;
pos[num] = i;
if(pos[num] != num && num != 0)
NumCount++;
}
while(NumCount > 0)
{
if(pos[0] == 0)
{
while(index < N)
{
if(pos[index] != index)
{
swap(pos[0],pos[index]);
swaptimes++;
break;
}
index++;
}
}
while(pos[0] != 0)
{
swap(pos[0],pos[pos[0]]);
NumCount--;
swaptimes++;
}
}
cout << swaptimes << endl;
}
}
Pat1067:Sort with Swap(0,*)的更多相关文章
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
- 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,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
- 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 ...
- 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 ...
- 7-16 Sort with Swap(0, i)(25 分)
7-16 Sort with Swap(0, i)(25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...
- 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 ...
- 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 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
随机推荐
- 漫谈程序员(十八)windows中的命令subst
漫谈程序员(十八)windows中的命令subst 用法格式 一.subst [盘符] [路径] 将指定的路径替代盘符,该路径将作为驱动器使用 二.subst /d 解除替代 三.不加任何参数键入 ...
- 停止预览时调用Camera.release(), 出现Method called after release()异常问题原因及解决办法
如下代码: private void stopPreview() { Log.w(TAG, "stopPreview(), _isPreviewing = " + _isPrevi ...
- 如何用代码禁用SpriteBuilder中创建的关节
这个目标是临时的禁用距离关节(distance joint). 不幸的是,你只可以无效化(通过删除的方式)一个关节. 所以,你必须通过代码创建一个新的距离关节实例并且赋予它之前删除关节(在Sprite ...
- 【48】java抽象类和接口的定义和区别
首先看看他们的区别: 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力. ...
- mybatis源码之BaseStatementHandler
/** * @author Clinton Begin */ public abstract class BaseStatementHandler implements StatementHandle ...
- ZooKeeper 数据模型
本文主要讲述ZooKeeper的数据模型,包括ZooKeeper的数据视图,节点的层次结构以及节点类型等基本属性.Zookeeper的视图结构类似标准的Unix文件系统,但是没有引入文件系统相关概念: ...
- SharePoint 用户控件编写的简单介绍
我们开发中,通常需要写各种各样的部件来实现我们的展示或者功能,下面就介绍下刚刚接触的QuickPart+用户控件的方式,算是自己的学习笔记,也和大家交流下心得. 1. 新建Web应用程序 2. 在项目 ...
- Java不走弯路教程(6.JDBC)
6.JDBC 在上一章,我们完成了MyDb数据库的简单的客户段调用.作为产品我们还封装了驱动程序,并且提供了统一的调用接口. 大家应该知道,市面上有多种数据库产品,比如Oracle,Mysql,DB2 ...
- Demo2
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- sqlplus 分析执行计划
转载 http://xm-koma.iteye.com/blog/1048451 对于oracle9i,需要手工设置plustrace角色,步骤如下: 1.在SQL>connect sys/密码 ...