1067. Sort with Swap(0,*) (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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,*)的更多相关文章

  1. PAT1067. Sort with Swap(0, *) (25) 并查集

    PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...

  2. pat1067. Sort with Swap(0,*) (25)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

随机推荐

  1. DBA_基本Bash语法汇总

    一.变量 1.变量命名可使用英文字母.数字和下划线,必须以英文字母开头,区分大小写. 2.每个shell都拥有自己的变量定义,彼此互不影响. 3.变量直接以等号赋值,注意等号两边不可留空,若等号右侧有 ...

  2. 新IO建立的聊天程序

    服务端: package com.net.scday3; import java.io.IOException; import java.net.InetSocketAddress; import j ...

  3. vector的简易实现

    vector的简易实现整理自<数据结构与算法分析–C++描述(第3版)>3.4节“向量的实现”.详细可参考<STL源码分析>4.2节. 具体实现代码如下: #ifndef VE ...

  4. 中文注释 MariaDB my.cnf 大型服务器配置模板

    文件如下所示,请根据需要进行修改: 翻译日期: 2014年5月22日 翻译人员: 铁锚 # /usr/share/mysql/my-huge.cnf # MariaDB 配置文件 示例模板(huge, ...

  5. Working with bounded Task Flows

      Working with Bounded Task Flows, Regions and Routers in JDeveloper 11g Purpose In this tutorial, y ...

  6. C语言之linux内核--BCD码转二进制与二进制转BCD码(笔试经典)

    在分析代码之前,我们先来了解一下,BCD码和二进制到底区别在哪? 学习过计算机原理的和数字电子技术这两门课的都会知道这两个到底是什么含义,也有的同学学过了,考过了,过了一段时间又忘记了,今天,我们通过 ...

  7. C语言笔试经典--求分数数列的和

    题目: 求数组的和    2   3/2   5/3   8/5  13/8   21/13  ...    求前20项的和 //求分数数列的和 #include<stdio.h> // ...

  8. Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!

    Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...

  9. lambda表达式初步

    // Lambda_test20140801.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <algorithm& ...

  10. css3-------:before和:after的作用

    1.:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: <!doctype html> ...