这是一道置换群的裸题=-=,先拿来试试手对着打,以后应该会更加熟练吧!
const maxn=;
var n,i,j,maxx,minx,now,len,cursum,tmin,sum:longint;
p:array[..maxn] of boolean;
agr,pos:array[..maxn] of longint;
function min(a,b:longint):longint;
begin
if a>b then exit(b)
else exit(a);
end;
begin
readln(n);
minx:=maxn;
for i:= to n do
begin
readln(agr[i]);
inc(pos[agr[i]]);
minx:=min(minx,agr[i]);
maxx:=maxx+agr[i]-min(maxx,agr[i]);
end;
//writeln(minx,' ',maxx);
for i:= to maxx do
inc(pos[i],pos[i-]);
fillchar(p,sizeof(p),true);
for i:= to n do
if p[i] then
begin
now:=i;
tmin:=agr[i];
len:=;
cursum:=;
while p[now] do
begin
inc(len);
tmin:=min(tmin,agr[now]);
inc(cursum,agr[now]);
p[now]:=false;
now:=pos[agr[now]];
end;
if len> then inc(sum,cursum);
if len> then inc(sum,min((len-)*tmin,minx*(len+)+tmin));
end;
writeln(sum);
end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)