问题描述
Alice 有一个数列ai。
但是她不喜欢这个数列,于是她决定随机交换其中两个数。
Alice 想知道,交换后的数列与原数列相同的数有多少个。请求出所有可能的
值。
输入
第一行一个正整数n,表示Alice 的数列的长度。
第二行n 个正整数,第i 个数表示ai,即交换前的数列的第i 项。
输出
一行若干个严格递增的正整数,用空格隔开,表示交换后的数列与原数列相同的数的个数。
样例输入
32
3 3
样例输出
1 3
样例解释
交换后可能的数列有2 3 3, 3 2 3, 3 3 2,个数分别是3, 1, 1。
算法讨论
暴力即可。
const
maxn=105;
var
a,c:array[0..maxn] of longint;
f:array[0..1000] of boolean;
b:array[0..maxn*maxn] of longint;
i,j,n,p:longint;
procedure qsort(l,r:longint);
var
i,j,m:longint;
begin
i:=l; j:=r;
m:=b[(l+r) div 2];
repeat
while b[i]<m do
inc(i);
while b[j]>m do
dec(j);
if i<=j
then begin
b[0]:=b[i];
b[i]:=b[j];
b[j]:=b[0];
inc(i); dec(j)
end;
until i>j;
if l<j
then qsort(l,j);
if i>r
then qsort(i,r)
end;
begin
assign(input,'array.in'); reset(input);
assign(output,'array.out'); rewrite(output);
read(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
begin
c:=a;
c[0]:=c[i]; c[i]:=c[j]; c[j]:=c[0];
inc(p);
b[p]:=n-2;
if c[i]=a[i]
then inc(b[p]);
if c[j]=a[j]
then inc(b[p])
end;
if p>1
then qsort(1,p);
for i:=1 to p do
if (f[b[i]]) and (b[i]<>0)
then continue
else if b[i]=0
then begin
write(0);
close(input); close(output);
halt
end
else begin
write(b[i],' ');
f[b[i]]:=true
end;
close(input); close(output)
end.
Pixiv ID:61325004