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

Source: Internet
Author: User

1067. Sort with Swap (0,*) (25) time limit MS Memory limit 65536 KB code length limit 16000 B award Program StandardAuthor Chen, Yue

Given any permutation of the numbers {0, 1, 2,..., N-1}, it's easy-to-sort them in increasing order. What if swaps (0, *) are the only operation and that's allowed to use? For example, to sort {4, 0, 2, 1, 3} We'll apply the swap operations in the following:

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 is asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative intege Rs.

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

Submit Code

Idea: Converting a sequence to a graph to form a key-value relationship

Examples: 3 5 7 2 6 4 9 0 8 1

Corresponding values: 0 1 2 3 4 5 6 7 8 9

corresponding keys: 3 5 7 2 6 4 9 0 8 1

3 Types of rings found:

A. Number of elements >1, containing 0 rings: 0-7-2-3-0

B. Number of elements >1, not including 0 rings: 1-9-6-4-5-1

C. Number of elements ==1 ring: 8

In fact, a picture is nothing more than the existence of the above 3 types of rings.

For a: number of switches = number of elements of the ring-1

For B: Number of switches = number of elements in the loop +1

For C: Interchange number =0

So, as long as the number of elements >1 the loop of the case can be. The total number of interchanges is then calculated based on whether the rings of these elements >1 contain element zeros.

The code is as follows:

1#include <cstdio>2#include <stack>3#include <cstring>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9map<int,int>ha;Ten BOOLvis[100005]; One voidDFS (intCurints) { Avis[cur]=true; -     if(ha[cur]==R) { -         return; the     } - DFS (ha[cur],s); - } - //The number of statistical points >1 rings, there are 0 in the number of exchanges is 1; no 0 in the ring, the number of exchanges is +1 + intMain () { -     //freopen ("D:\\input.txt", "R", stdin); +     intCount,n; Ascanf"%d",&n); atCount=N; -     intI,num; -      for(i=0; i<n;i++){ -scanf"%d",&num); -         if(num==i) {//Remove the number of elements 0, the number of elements remaining -vis[num]=true; incount--; -         } toha[num]=i; +     } -     //The rest of the ring, in addition to the ring containing element 0, the number of exchanges of the other rings = the number of elements of the ring +1 the      for(i=0; i<n;i++){ *         if(!Vis[i]) { $ DFS (i,i);Panax Notoginsengcount++; -         } the     } +     if(ha[0]!=0){//If 0 is not a separate ring, indicating that there is an element number >1 the ring contains 0, then this ring in 34 lines add 2, here minus Acount-=2; the     } +printf"%d\n", count); -     return 0; $}

pat1067. Sort with Swap (0,*)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.