Rokua P1327 Sequence Order

Source: Internet
Author: User

P1327 Sequence OrderTitle Description

Given a sequence {an}, this sequence satisfies Ai≠aj (I≠J), and now requires you to order this sequence from small to large, each time you allow you to exchange any one of them, how many times do I need to exchange?

input/output formatInput Format:

The first line, positive integer n (n<=100,000).

Following several lines, total n number, separated by a space, indicating the sequence {an}, arbitrary -2^31<ai<2^31-1.

output Format:

Only one row, containing a number, represents the minimum number of interchanges.

input/Output sampleInput Sample # #: Copy
88 23 4 16 77-5 53 100
Output Example # #: Replication
5

Idea: Greed.

#include <map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;map<int,int>ID;intN,ans;intnum[100010],pos[100010];intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", &num[i]), id[num[i]]=i,pos[i]=Num[i]; Sort (POS+1, pos+1+N);  for(intI=1; i<=n;i++)        if(num[i]!=Pos[i]) {Num[id[pos[i] ]=Num[i]; Id[num[i]]=Id[pos[i]]; Num[i]=Pos[i]; Ans++; } cout<<ans;}

Rokua P1327 Sequence Order

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.