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. 高水线 High water mark(HWM)

    所有的Oracle表都有一个容纳数据的上限(很像一个水库历史最高的水位),我们把这个上限称为“High water mark"或HWM.这个HWM是一个标记(专门有一个数据块来记录高水标记等 ...

  2. PHP的安装配置

    一.安装 PHP的安装可以很简单的使用yum命令进行安装. #添加php7.0源(这是centos7的命令,centos6.5的命令不同,不要照搬)rpm -Uvh https://dl.fedora ...

  3. Python列表推导式中使用if-else

    data_list=[] col=["a", "b", "c", "d"] jdata={"a":1 ...

  4. UltraEdit常用快捷键

    UltraEdit是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII码,可以取代记事本,内建英文单字检查.C++及VB指令突显,可同时编辑多个文件,而且即使开启很大的文件速度也不会慢. 说 ...

  5. enovia PLM : add new value to SPEO

    Solution: Modify LUX_SPEO attribute in PLM Modify D_SPEO attribute in SAP , Login sap system F3 Tcod ...

  6. 事务管理ACID

    事务是由一组SQL语句组成的逻辑处理单元,事务具有以下4个属性,通常简称为事务的ACID属性. ACID是Atomic(原子性) Consistency(一致性) Isolation(隔离性) Dur ...

  7. JSONObject.toJSONString(map)

    Map<Integer, List<Integer>> map = new LinkedHashMap<>(); map.put(1,ddzCard.getOneS ...

  8. Codeforces 343D 线段树

    题意:给你一颗以点1为根的数,有两种操作,一种是把x及其子树的所有点都灌满水,一种是把x及其所有祖先都放空水,一种是询问,问某个点里有没有水? 思路:看网上大多数是树剖,但实际上5e5的数据树剖还是有 ...

  9. JSON对象排序并生成URL参数

    1、for in function jsontourl(param) { let params = {}, data=[]; let arr = Object.keys(param).sort(); ...

  10. html浮动小问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...