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:



注意点:1.a!=0别忘    
    2.k必须放在外面,否则超时!
#include<bits/stdc++.h>
using namespace std; const int maxn = 100010; int n; int pos[maxn]; int main(){ cin>>n; int a; int left=n-1,ans=0; for(int i=0;i<n;i++){
cin>>a;
pos[a]=i; if(pos[a]==a&&a!=0)//a!=0别忘
left--;
} int k=1; //k必须放在外面,否则超时 while(left>0){
if(pos[0]==0){ while(k<n){
if(pos[k]!=k){
swap(pos[0],pos[k]);
ans++;
break;
} k++;
} }else{
swap(pos[0],pos[pos[0]]);
left--;
ans++;
} } cout<<ans<<endl; }

  

1067 Sort with Swap(0, i) (25 分)的更多相关文章

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

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

  3. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

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

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

  6. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

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

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

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

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

随机推荐

  1. 导入csv 到mysql数据库

    1.查询导入数据存放位置 show variables like '%secure%'; +--------------------------+-----------------------+ | ...

  2. JS-拷贝对象

    As it is well known to us all, 拷贝对象分为浅拷贝和深拷贝,深拷贝只会复制地址,深拷贝才会复制内容,那么 JS 如何进行这两种拷贝呢? # 浅拷贝 ## Object.a ...

  3. js关闭当前页面清除session

    js关闭当前页面清除session 普通页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  4. (转)spring ioc原理(看完后大家可以自己写一个spring)

    原文地址:https://blog.csdn.net/it_man/article/details/4402245 最近,买了本Spring入门书:spring In Action .大致浏览了下感觉 ...

  5. linux 杂七杂八

    一."init"是内核启动的第一个用户空间程序(PID=1),也是所有用户态进程的"大总管":所有内核态进程的大总管是PID=2的[kthreadd]: 二.v ...

  6. PHP中使用raw格式发送POST请求

    如果请求的参数格式是原生(raw)的内容,应该如何为程序构造一个POST请求函数呢? function http_post($url, $data_string) { $ch = curl_init( ...

  7. python中使用动态库

    首先,创建一个简单的动态库编程生成dll.so:   gcc -fPIC -O2 -shared dll.c -o dll.soC文件:dll.c  如下 #include <stdio.h&g ...

  8. grep中正则表达式使用尖括号表示一个单词

    比如 grep '\<bin\>' /etc/passwd --color

  9. Spring集成RMI实现远程调用

    前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...

  10. Spring定时任务的几种实现(转)

    转自:http://gong1208.iteye.com/blog/1773177 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几 ...