[SGU]108. Self-numbers II

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值