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. 【面试笔试算法】Program 3 : Complicated Expression(网易游戏笔试题)

    时间限制:50000ms 单点时限:5000ms 内存限制:256MB 描述 在lisp语言中,表达式都用前缀法表示,例如,1 + 2 在lisp中使用(+ 1 2)来表示,其中,表达式的括号是必需的 ...

  2. 细说Android事件传递

    一.View的dispatchTouchEvent和onTouchEvent 探讨Android事件传递机制前,明确android的两大基础控件类型:View和ViewGroup.View即普通的控件 ...

  3. Linux - grep的一些进阶选项

    [root@www ~]# grep [-A] [-B] [--color=auto] '搜寻字串' filename 选项与参数: -A :后面可加数字,为 after 的意思,除了列出该行外,后续 ...

  4. saiku的安装教程

    Saiku是一个模块化的开源分析套件,它提供轻量级的OLAP(联机分析处理),并且可嵌入.可扩展.可配置. 环境准备 1.JDK5及以上版本. 2.Saiku Server最新版本,下载地址:http ...

  5. Leetcode_252_Implement Stack using Queues

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598773 Implement the followin ...

  6. 【Qt编程】基于Qt的词典开发系列<五>--无边框窗口的拖动

    在上一篇文章中,我们讲述了如何进行无边框窗口的缩放与拖动,而在一些情况下,我们的窗口只需要进行拖动也不需要改变其大小,比如:QQ的登录窗口.本来在上一篇文章中已经讲述了如何进行窗口的拖动,但是却与窗口 ...

  7. 【39】FlexboxLayout使用介绍

    FlexboxLayout介绍: Flexbox 也称为弹性盒子模型 或伸缩盒子模型,广泛用于前端开发,做过前端 web 的都知道Bootstrap 中有一套强大的 CSS Grid网格样式.Boot ...

  8. init()和onEnter()方法的区别

    init()和onEnter()这俩个方法都是CCNode的方法.其区别如下: 1.其被调用的顺序是先init(),后onEnter(). 2.init()在类的初始化时只会调用一次. 3.onEnt ...

  9. python snownlp情感分析简易demo

    SnowNLP是国人开发的python类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和T ...

  10. jbpm 工作流(二)

    1           概述 本文主要介绍如何将JBPM+Struts+Spring+Hibernate整合在一块.并通过一个简单实例来说明.此实例为一个申请审批的简单流程,并将申请人和审批人记录到数 ...