結果

問題 No.596 郵便配達
ユーザー ikdikd
提出日時 2017-12-29 12:55:58
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 731 ms / 5,000 ms
コード長 2,202 bytes
コンパイル時間 3,529 ms
コンパイル使用メモリ 76,624 KB
実行使用メモリ 82,512 KB
最終ジャッジ日時 2023-09-03 17:50:16
合計ジャッジ時間 5,865 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
11,268 KB
testcase_01 AC 8 ms
12,784 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 353 ms
36,456 KB
testcase_05 AC 731 ms
72,880 KB
testcase_06 AC 79 ms
10,504 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 324 ms
33,256 KB
testcase_09 AC 1 ms
4,380 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 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 710 ms
82,512 KB
testcase_21 AC 713 ms
81,276 KB
testcase_22 AC 716 ms
82,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import core.stdc.stdio, std.exception;

  int n, m; scanf("%d %d", &n, &m);
  auto xs=new int[](m), ls=new int[](m);
  auto ys=new int[][](m);
  foreach(i; 0..m){
    scanf("%d %d", &xs[i], &ls[i]);
    foreach(j; 0..ls[i]){int y; scanf("%d", &y); ys[i]~=y;}
  }

  // enforce(false);

  int mn=1_000_000_000;
  mn=min(mn, solve(n, m, xs, ls, ys));
  foreach(i; 0..m){
    xs[i]=(n-xs[i]-1);
    foreach(j; 0..ls[i]) ys[i][j]=(n-ys[i][j]-1);
  }
  mn=min(mn, solve(n, m, xs, ls, ys));

  writeln(mn);
}

int solve(int n, int m, int[] xs, int[] ls, int[][] ys){
  import std.algorithm, std.stdio;
  auto c=new int[](n);
  int l=n, r=-1;
  foreach(i; 0..m){
    l=min(l, xs[i]); r=max(r, xs[i]);
    foreach(y; ys[i]){
      l=min(l, y); r=max(r, y);
      if(y<xs[i]) c[y]++, c[xs[i]]--;
    }
  }
  foreach(i; 1..n) c[i]+=c[i-1];
  auto rec=new int[][][](2, 3, 3); // R, LR, RLR
                                   // (st, ed)=(F, F), (T, F), (T, T)
  const inf=1_000_000_000;
  foreach(t; 0..2)foreach(k; 0..3) fill(rec[t][k], inf);
  rec[l&1][1][0]=2;
  if(c[l]==0) rec[l&1][0][1]=1;
  for(auto i=l+1; i<r; i++){
    auto t=i&1;
    foreach(k; 0..3) fill(rec[t][k], inf);
    
    foreach(j; 0..3){
      chmin(rec[t][2][j], rec[t^1][0][j]+3);
      chmin(rec[t][1][j], rec[t^1][1][j]+2);
      chmin(rec[t][2][j], rec[t^1][2][j]+3);
      if(c[i]==0){
        if(j<=1) chmin(rec[t][0][j], rec[t^1][0][j]+1);
        if(j==0) chmin(rec[t][0][j+1], rec[t^1][1][j]+1);
        chmin(rec[t][0][j], rec[t^1][2][j]+1);
      }
      if(j==1) chmin(rec[t][1][j+1], rec[t^1][0][j]+2);
    }
  }

  int ret=inf;
  chmin(ret, min(rec[(r&1)^1][0][2], rec[(r&1)^1][1][2])); 
  chmin(ret, min(rec[(r&1)^1][0][1], rec[(r&1)^1][1][1])); // ウィニングランする感じ
  chmin(ret, rec[(r&1)^1][1][0]); // 閉路
  return ret;
}

void chmin(T)(ref T l, T r){
  if(l>r) l=r;
}

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) write(e, " ");
  writeln();
}
0