結果

問題 No.615 集合に分けよう
ユーザー ikdikd
提出日時 2017-12-15 23:54:55
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 1,133 ms
コンパイル使用メモリ 103,200 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2023-09-03 17:39:00
合計ジャッジ時間 2,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,500 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 10 ms
4,660 KB
testcase_16 AC 26 ms
6,348 KB
testcase_17 AC 46 ms
12,440 KB
testcase_18 AC 46 ms
12,672 KB
testcase_19 AC 45 ms
12,628 KB
testcase_20 AC 40 ms
7,140 KB
testcase_21 AC 34 ms
6,856 KB
testcase_22 AC 26 ms
6,392 KB
testcase_23 AC 22 ms
6,924 KB
testcase_24 AC 38 ms
7,420 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 23 ms
12,284 KB
testcase_28 AC 23 ms
12,296 KB
testcase_29 AC 29 ms
12,636 KB
testcase_30 AC 27 ms
9,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.typecons;

  int n, k; rd(n, k);
  auto a=readln.split.to!(long[]);

  sort(a);
  alias T=Tuple!(long, "d", int, "i");
  auto dif=new T[](n-1);
  foreach(i; 0..(n-1)) dif[i]=T(a[i+1]-a[i], i);
  sort!((x, y)=>(x.d<y.d))(dif);

  auto g=new int[][](n, 0);
  foreach(i; 0..(n-k)){
    auto e=dif[i];
    g[e.i]~=(e.i+1);
    g[e.i+1]~=e.i;
  }

  auto vis=new bool[](n);
  Tuple!(long, long) min_max(int i, int pre){
    vis[i]=true;
    auto mn=a[i], mx=a[i];
    foreach(j; g[i])if(j!=pre){
      auto res=min_max(j, i);
      mn=min(mn, res[0]);
      mx=max(mx, res[1]);
    }
    return tuple(mn, mx);
  }
  long tot=0;
  foreach(i; 0..n)if(vis[i]==false){
    auto res=min_max(i, -1);
    tot+=(res[1]-res[0]);
  }
  writeln(tot);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) stderr.write(e, " ");
  stderr.writeln();
}
0