题解,仅供参考,请勿滥用!

var e:array[1..10] of longint;
w,d:array[1..50000] of longint;
i,m,n,j:longint;

procedure ss(x,y:longint);
var ii,jj,s,o,aa:longint;
begin
ii:=x;jj:=y;
s:=w[(x+y) shr 1];
aa:=d[(x+y) shr 1];
repeat
while (w[ii]>s) or ((w[ii]=s) and (d[ii]<aa)) do inc(ii);
while (w[jj]<s) or ((w[jj]=s) and (d[jj]>aa)) do dec(jj);
if ii<=jj then begin
o:=w[ii];w[ii]:=w[jj];w[jj]:=o;
o:=d[ii];d[ii]:=d[jj];d[jj]:=o;
inc(ii);dec(jj);
end;
until ii>jj;
if x<jj then ss(x,jj);
if ii<y then ss(ii,y);
end;

begin
readln(n,m);
for i:=1 to 10 do read(e[i]);
readln;
for i:=1 to n do read(w[i]);
for i:=1 to n do d[i]:=i;
ss(1,n);
for i:=1 to n do w[i]:=w[i]+e[(i-1) mod 10+1];
ss(1,n);
if m=0 then halt;
write(d[1]);
for i:=2 to m do write(' ',d[i]);
readln;
end.

5 条评论

  • @ 2016-11-07 10:05:34

    我的代码和你几乎一样,为啥就过不了呢???
    Free Pascal Compiler version 3.0.0 [2015/11/16] for i386
    Copyright (c) 1993-2015 by Florian Klaempfl and others
    Target OS: Win32 for i386
    Compiling foo.pas
    Linking foo.exe
    47 lines compiled, 0.0 sec, 28224 bytes code, 1268 bytes data
    测试数据 #0: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 1204 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
    测试数据 #4: WrongAnswer, time = 0 ms, mem = 1200 KiB, score = 0
    测试数据 #5: RuntimeError, time = 0 ms, mem = 1204 KiB, score = 0
    测试数据 #6: RuntimeError, time = 15 ms, mem = 1200 KiB, score = 0
    测试数据 #7: RuntimeError, time = 15 ms, mem = 1200 KiB, score = 0
    测试数据 #8: RuntimeError, time = 15 ms, mem = 1200 KiB, score = 0
    测试数据 #9: RuntimeError, time = 15 ms, mem = 1204 KiB, score = 0
    RuntimeError, time = 60 ms, mem = 1204 KiB, score = 40

    program P1282;
    var
        n,k,i:longint;
        w,ar:array[1..50000] of longint;
        e:array[1..10] of longint;
    procedure sort(l,r: longint);
    var
      i,j,x,y,temp: longint;
    begin
      i:=l;
      j:=r;
      x:=w[(l+r) div 2];
        y:=ar[(l+r) div 2];
        repeat
        while (w[i]>x) or ((w[i]=x) and (ar[i]<y)) do inc(i);
        while (x>w[j]) or ((w[i]=x) and (ar[i]>y)) do dec(j);
        if not(i>j) then
        begin
                temp:=w[i];
          w[i]:=w[j];
                w[j]:=temp;
                temp:=ar[i];
                ar[i]:=ar[j];
                ar[j]:=temp;
          inc(i);
          dec(j);
        end;
      until i>j;
        if l<j then sort(l,j);
        if i<r then sort(i,r);
    end;
    begin
        readln(n,k);
        for i:=1 to 10 do read(e[i]);
        readln;
        for i:=1 to n do
            begin
                read(w[i]);
                ar[i]:=i;
            end;
        sort(1,n);
        for i:=1 to n do w[i]:=w[i]+e[(i-1) mod 10 +1];
        sort(1,n);
        for i:=1 to k do write(ar[i],' ');
    end.
    
    • @ 2017-01-20 16:32:02

      编译成功
      Free Pascal Compiler version 3.0.0 [2015/11/16] for i386
      Copyright (c) 1993-2015 by Florian Klaempfl and others
      Target OS: Win32 for i386
      Compiling foo.pas
      Linking foo.exe
      40 lines compiled, 0.0 sec, 28048 bytes code, 1268 bytes data
      测试数据 #0: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #1: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #2: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #3: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #4: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #5: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
      测试数据 #6: Accepted, time = 31 ms, mem = 1200 KiB, score = 10
      测试数据 #7: Accepted, time = 15 ms, mem = 1204 KiB, score = 10
      测试数据 #8: Accepted, time = 31 ms, mem = 1200 KiB, score = 10
      测试数据 #9: Accepted, time = 31 ms, mem = 1200 KiB, score = 10
      Accepted, time = 108 ms, mem = 1204 KiB, score = 100
      var
      a, c:array[1..50000] of longint;
      b:array[1..10] of longint;
      n, k, i:longint;
      procedure qsort(l, r:longint);
      var
      i, j, p, m, t:longint;
      begin
      i:=l;
      j:=r;
      p:=a[(l+r) div 2];
      m:=c[(l+r) div 2];
      repeat
      while (a[i]>p) or ((a[i]=p) and (c[i]<m)) do inc(i);
      while (a[j]<p) or ((a[j]=p) and (c[j]>m)) do dec(j);
      if i<=j then begin
      t:=a[i];
      a[i]:=a[j];
      a[j]:=t;
      t:=c[i];
      c[i]:=c[j];
      c[j]:=t;
      inc(i);
      dec(j)
      end;
      until i>j;
      if i<r then qsort(i, r);
      if j>l then qsort(l, j)
      end;
      begin
      read(n, k);
      for i:=1 to 10 do read(b[i]);
      for i:=1 to n do read(a[i]);
      for i:=1 to n do c[i]:=i;
      qsort(1, n);
      for i:=1 to n do inc(a[i], b[(i-1) mod 10+1]);
      qsort(1, n);
      if k=0 then halt;
      write(c[1]);
      for i:=2 to k do write(' ', c[i])
      end.

  • @ 2015-04-22 18:07:28

    。。。

  • @ 2015-04-20 20:20:40

    为什么我的快排必须要用2次才能按编号排好序???

    能帮我看看么,data【i,1】是号次,2是W。change是我自己写的交换函数

    procedure qsort(l,r:longint);
    var mid,a,b,i:longint;
    begin
    mid:=data[(l+r) div 2,2]; a:=l; b:=r;
    repeat
    while (data[a,2]>mid) or ((data[a,2]=mid) and (data[a,1]<data[(l+r) div 2,1])) do inc(a);
    while (data[b,2]<mid) or ((data[b,2]=mid) and (data[b,1]>data[(l+r) div 2,1])) do dec(b);
    if a<=b then
    begin
    change(data[a,2],data[b,2]);
    change(data[a,1],data[b,1]);
    inc(a); dec(b);
    end;
    until a>b;
    if a<r then qsort(a,r);
    if l<b then qsort(l,b);
    end;

    • @ 2015-04-20 20:30:05

      诶,怪了,我弄了个i=data[(l+r) div 2,1],替换掉了原句的data[(l+r) div 2,1]结果就对了,为什么?

    • @ 2015-04-20 20:37:59

      已经解决了。原来repeat里面l+r div 2 的值会变化。导致快排出错。诶- -算是吃一堑长一智

  • @ 2015-03-16 12:09:47

    ok

  • @ 2014-11-08 20:38:44

    掌握一种O(nlogn)的排序就ok了

  • 1

信息

ID
1282
难度
6
分类
其他 | 排序 点击显示
标签
递交数
3784
已通过
988
通过率
26%
被复制
6
上传者