Analysis
我的做法是先把输入的k个数排个序,然后找到1..n中的Self-number,对每个输入找到答案,重新按输入顺序排序后输出。因为内存限制较严格,is_sn数组是循环使用的。
Accepted Code
type
mytype=record
data,num,ans:longint;
end;
var
s:array[1..6000] of mytype;
sum:array[1..9999] of longint;
is_sn:array[0..127] of boolean;
n,k,i,tot,now,tmp,d:longint;
procedure sort1(l,r:longint);
var
i,j:longint;
tmp,mid:mytype;
begin
i:=l;
j:=r;
mid:=s[(l+r) shr 1];
repeat
while s[i].data<mid.data do
inc(i);
while s[j].data>mid.data do
dec(j);
if not (i>j) then
begin
tmp:=s[i];
s[i]:=s[j];
s[j]:=tmp;
inc(i);
dec(j);
end;
until i>j;
if l<j then
sort1(l,j);
if i<r then
sort1(i,r);
end;
procedure sort2(l,r:longint);
var
i,j:longint;
tmp,mid:mytype;
begin
i:=l;
j:=r;
mid:=s[(l+r) shr 1];
repeat
while s[i].num<mid.num do
inc(i);
while s[j].num>mid.num do
dec(j);
if not (i>j) then
begin
tmp:=s[i];
s[i]:=s[j];
s[j]:=tmp;
inc(i);
dec(j);
end;
until i>j;
if l<j then
sort2(l,j);
if i<r then
sort2(i,r);
end;
begin
read(n,k);
for i:=1 to k do
begin
read(s[i].data);
s[i].num:=i;
end;
fillchar(is_sn,sizeof(is_sn),true);
sort1(1,k);
tot:=0;
now:=1;
for i:=1 to 9999 do
begin
tmp:=i;
sum[i]:=0;
while tmp>0 do
begin
sum[i]:=sum[i]+tmp mod 10;
tmp:=tmp div 10;
end;
end;
for i:=1 to n do
begin
if is_sn[i and 127] then
begin
inc(tot);
while (now<=k) and (s[now].data=tot) do
begin
s[now].ans:=i;
inc(now);
end;
end;
tmp:=i;
d:=i;
while tmp>0 do
begin
d:=d+sum[tmp mod 10000];
tmp:=tmp div 10000;
end;
is_sn[i and 127]:=true;
is_sn[d and 127]:=false;
end;
sort2(1,k);
writeln(tot);
for i:=1 to k-1 do
write(s[i].ans,' ');
writeln(s[k].ans);
end.