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