题目信息

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

时间限制150 ms

内存限制65536 kB

代码长度限制16000 B

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 (<=10^5) 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

解题思路

首位为0,找出第一个未排序的数的位置进行交换

首位非0。与该数应处的位置交换

AC代码

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a, b;
int main()
{
int n, t, c = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", &t);
a.push_back(t);
}
int cnt = 0;
while (c < n){ //序列排序未结束
if (0 == a[0]){ //首位为0,找出第一个未排序的数的位置
for (; c < n && c == a[c]; ++c)
continue;
if (c >= n) break;
swap(a[0], a[c]);
++cnt;
}else{ //首位非0。与应处于的位置交换
swap(a[0], a[a[0]]);
++cnt;
}
}
printf("%d\n", cnt);
return 0;
}

个人游戏推广:

《10云方》与方块来次消除大战!

1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise的更多相关文章

  1. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

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

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

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

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

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

  5. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

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

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

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

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

随机推荐

  1. iOS开发-文件管理之多的是你不知道的事(一)

    郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  2. 纪念2014 TI DSP大奖赛

    偶然发现TI官网有新闻报道大奖赛,还有沈洁女士给我们颁奖的照片.纪念一下. 第六届TI DSP及嵌入式大奖赛决赛暨颁奖典礼在厦门大学成功举行 Frances Han  2013-2014 TI DSP ...

  3. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  4. UVA 10859 - Placing Lampposts 树形DP、取双优值

                              Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...

  5. 在外星人电脑上安装windows10和ubuntu16.04双系统小记

    最近刚刚入手了一台Alienware Aurora R6,买这货的主要目的是为了研究Deep Learning.之所以没有买组装机的原因,主要是担心组装机的不稳定,而实验经费中的设备费也还相对充足,于 ...

  6. redis持久化,rdb,aof

    RDB(Redis DataBase) AOF(Append Only File) 周阳语录:能撑过面试经理头一分钟最重要.头一分钟,决定人家还是否想跟你继续聊下去. RDB RDB就是在指定的时间内 ...

  7. hmm CDN检测

    # -*- coding:utf-8 -*- import sys import re from hmmlearn import hmm import numpy as np from sklearn ...

  8. UESTC--1263--The Desire of Asuna(贪心)

    The Desire of Asuna Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Su ...

  9. Java-MyBatis:MyBatis 中 in 的用法

    ylbtech-Java-MyBatis-杂项:MyBatis  中  in 的用法 1.返回顶部 1. foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元 ...

  10. 从git源码安装zabbix-agent

    从网上随便找了几个image,都是从svn下载的代码,根本跑不了,于是自己重新做了个,从git上拿的源码,只是build出来的image有点大,有时间再优化了 Dockerfile FROM cent ...