P1697: [Usaco2007 Feb]Cow Sorting牛排序
这是一道置换群的裸题=-=,先拿来试试手对着打,以后应该会更加熟练吧!
1 const maxn=100001;
2 var n,i,j,maxx,minx,now,len,cursum,tmin,sum:longint;
3 p:array[0..maxn] of boolean;
4 agr,pos:array[0..maxn] of longint;
5 function min(a,b:longint):longint;
6 begin
7 if a>b then exit(b)
8 else exit(a);
9 end;
10 begin
11 readln(n);
12 minx:=maxn;
13 for i:=1 to n do
14 begin
15 readln(agr[i]);
16 inc(pos[agr[i]]);
17 minx:=min(minx,agr[i]);
18 maxx:=maxx+agr[i]-min(maxx,agr[i]);
19 end;
20 //writeln(minx,' ',maxx);
21 for i:=1 to maxx do
22 inc(pos[i],pos[i-1]);
23 fillchar(p,sizeof(p),true);
24 for i:=1 to n do
25 if p[i] then
26 begin
27 now:=i;
28 tmin:=agr[i];
29 len:=0;
30 cursum:=0;
31 while p[now] do
32 begin
33 inc(len);
34 tmin:=min(tmin,agr[now]);
35 inc(cursum,agr[now]);
36 p[now]:=false;
37 now:=pos[agr[now]];
38 end;
39 if len>1 then inc(sum,cursum);
40 if len>2 then inc(sum,min((len-2)*tmin,minx*(len+1)+tmin));
41 end;
42 writeln(sum);
43 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)