hahai21688

hahai21688

@hahai21688
Hiệu năng
1,05 #1394
Đã giải
0
Các kỳ thi đang tham gia
0/ 3
Đóng góp
0
§ giới thiệu

Thông tin

program baitrongcay; type mang=record gt:longint; kt:boolean; end; var n,k,i,j,min,max,t,tam,vt,nd,nc,kq,u,v:longint; a:array[0..1000000] of longint; b:array[1..1000000] of mang; procedure sort(l,r: longint); var i,j,x,y: longint; begin i:=l; j:=r; x:=a[(l+r) div 2]; repeat while a[i]<x do inc(i); while x<a[j] do dec(j); if not(i>j) then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; inc(i); j:=j-1; 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 n do read(a[i]); sort(1,n); j:=0; for i:=1 to n do if a[k]<>a[i] then begin inc(j); b[j].gt:=a[i]; b[j].kt:=true; end; t:=j; tam:=a[k]; vt:=0; for i:=k-1 downto 1 do begin min:=high(longint); for j:=1 to t do if (min>abs(tam-b[j].gt)) and (b[j].kt) then begin min:=abs(tam-b[j].gt); b[j].kt:=false; vt:=j; end; if min<>high(longint) then begin kq:=kq+min; tam:=b[vt].gt; end; for j:=1 to vt-1 do b[j].kt:=true; end; tam:=a[k]; for i:=k+1 to n do begin min:=high(longint); for j:=1 to t do if (b[j].kt) and (min>abs(tam-b[j].gt)) then begin min:=abs(tam-b[j].gt); b[j].kt:=false; vt:=j; end; if min<>high(longint) then begin kq:=kq+min; tam:=b[vt].gt; end; for u:=vt+1 to t do b[u].kt:=true; end; writeln(kq); end.

Hoạt động

gnatmake 12.2.0 a68g 3.1.2 nasm 2.16.1 as_x64 2.46 awk 1.3.4 gcc 16.1.0 csc 6.12.0.200 g++ 16.1.0 g++-themis 16.1.0 g++17 16.1.0 g++20 16.1.0 g++23 16.1.0 clang++ 22.1.6 dmd 2.112.0 dart 3.12.1 gforth 0.7.3 gfortran 12.2.0 go 1.26.3 groovyc 5.0.6 javac 25.0.3 node 26.2.0 kotlinc 2.3.21 sbcl 2.2.9 lua 5.4.8 nim 2.2.10 fpc 3.2.2 fpc-themis 3.2.2 perl 5.36.0 php 8.5.6 pike 8.0 pypy3 7.3.23 python3 3.14.5 racket 8.7 ruby 4.0.5 rustc 1.96.0 csc 5.3.0 ctoj-scratch 0.0.1 sed 4.9 tclsh 8.6 bun 1.3.14 deno 2.8.1 v 0.5.1 zig 0.16.0