1067 Sort with Swap(0, i)

news/2024/11/8 12:58:23

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

#include<iostream>
using namespace std;
int a[100001];
int main(){
   int n;
   cin>>n;
   int t;
   for(int i=0;i<n;i++){
      cin>>t;
      a[t]=i;
   }
   int cnt=0;
   for(int i=1;i<n;i++){
      if(i!=a[i]){
         while(a[0]!=0){
            swap(a[0],a[a[0]]);
            cnt++;
         }
         if(i!=a[i]){
            swap(a[0],a[i]);
            cnt++;
         }
      }
   }
   cout<<cnt;
   
   return 0;
}

http://www.niftyadmin.cn/n/4636571.html

相关文章

1090 Highest Price in Supply Chain

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppl…

linux 设备文件和设备之间联系的建立

<设备驱动模型> 注&#xff1a;几乎所有的设备结构体都包含"strcut kobject kobj"和"srtuct list_head list"该结构体。struct kobject kobj: 该结构体用于构建Linux设备驱动模型的模型建立 struct list_head { struct list_head *prev,*next; }; 该…

mysql-存储过程-触发器-事务---4

本节所讲内容&#xff1a; 存储过程 触发器事务一、存储过程 什么是存储过程 大多数SQL语句都是针对一个或多个表的单条语句。并非所有的操作都怎么简单。经常会有一个完整的操作需要多条才能完成。存储过程&#xff08;Stored Procedure&#xff09;是在大型数据库系统中&…

Linux系统安装与初用 实验报告

南京信息工程大学实验报告 一、实验目的 1.掌握 linux 系统的安装方法 2.理解 linux 系统安装过程中涉及的基础知识 3.熟悉 linux 系统的操作环境 4.尝试简单的 linux shell 命令 二、实验准备 1. 围绕下述问题结合教材、课件、互联网学习指定内容。 问题&#xff1a; &#xf…

Maven学习笔记三(Eclipse创建Maven项目)

配置 Eclipse Maven 环境1.配置 Manen 地址将下载的Maven导入进来&#xff0c;然后勾选使用2.设置 setting.xml 地址选中Maven下conf目录下的settings.xml&#xff0c;然后local Repository会自动识别出设置的local Repository创建 Maven 项目选中模板 &#xff08;如果是创建w…

1058 A+B in Hogwarts easy

If you are a fan of Harry Potter, you would know the world of magic has its own currency system — as Hagrid explained it to Harry, “Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it’s easy enough.” Your job is to write a progr…

Java-API简析_java.io.InputStreamReader类(基于 Latest JDK)(浅析源码)

【版权声明】未经博主同意&#xff0c;谢绝转载&#xff01;&#xff08;请尊重原创&#xff0c;博主保留追究权&#xff09; https://blog.csdn.net/m0_69908381/article/details/131734299 出自【进步*于辰的博客】 因为我发现目前&#xff0c;我对Java-API的学习意识比较薄弱…

前端每周清单第 55 期: MobX 4 特性概览,iOS Hacks 分享, 分布式事务详解

前端每周清单第 55 期: MobX 4 特性概览&#xff0c;iOS Hacks 分享, 分布式事务详解 作者&#xff1a;王下邀月熊 编辑&#xff1a;徐川 前端每周清单专注大前端领域内容&#xff0c;以对外文资料的搜集为主&#xff0c;帮助开发者了解一周前端热点&#xff1b;分为新闻热点、…