結果

問題 No.1508 Avoid being hit
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-05-14 23:50:00
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 152 ms / 3,000 ms
コード長 2,931 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 111,248 KB
実行使用メモリ 21,728 KB
最終ジャッジ日時 2023-09-04 12:40:34
合計ジャッジ時間 8,851 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
15,716 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 87 ms
16,796 KB
testcase_17 AC 51 ms
11,076 KB
testcase_18 AC 42 ms
7,952 KB
testcase_19 AC 73 ms
11,800 KB
testcase_20 AC 107 ms
20,452 KB
testcase_21 AC 70 ms
11,652 KB
testcase_22 AC 90 ms
17,860 KB
testcase_23 AC 97 ms
17,556 KB
testcase_24 AC 152 ms
21,728 KB
testcase_25 AC 139 ms
15,024 KB
testcase_26 AC 11 ms
4,376 KB
testcase_27 AC 71 ms
10,752 KB
testcase_28 AC 70 ms
10,504 KB
testcase_29 AC 141 ms
20,928 KB
testcase_30 AC 76 ms
11,256 KB
testcase_31 AC 125 ms
14,908 KB
testcase_32 AC 136 ms
15,004 KB
testcase_33 AC 38 ms
6,984 KB
testcase_34 AC 120 ms
14,300 KB
testcase_35 AC 13 ms
4,384 KB
testcase_36 AC 69 ms
10,112 KB
testcase_37 AC 32 ms
6,224 KB
testcase_38 AC 97 ms
12,212 KB
testcase_39 AC 64 ms
8,908 KB
testcase_40 AC 127 ms
19,840 KB
testcase_41 AC 74 ms
9,928 KB
testcase_42 AC 128 ms
18,248 KB
testcase_43 AC 148 ms
21,656 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }




void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const Q = readInt();
      auto A = new int[Q];
      foreach (i; 0 .. Q) {
        A[i] = readInt();
      }
      auto B = new int[Q];
      foreach (i; 0 .. Q) {
        B[i] = readInt();
      }
      
      alias Il = Tuple!(int, "l", int, "r");
      auto dp = new Il[][Q + 1];
      dp[Q] = [Il(1, N)];
      foreach_reverse (i; 0 .. Q) {
        auto crt = dp[i + 1].dup;
        foreach (x; [A[i], B[i]]) {
          Il[] nxt;
          foreach (p; crt) {
            if (p.l <= x && x <= p.r) {
              if (p.l < x) nxt ~= Il(p.l, x - 1);
              if (x < p.r) nxt ~= Il(x + 1, p.r);
            } else {
              nxt ~= p;
            }
          }
          crt = nxt;
        }
        foreach (ref p; crt) {
          chmax(--p.l, 1);
          chmin(++p.r, N);
        }
        {
          Il[] nxt;
          foreach (p; crt) {
            if (!nxt.empty && nxt[$ - 1].r + 1 >= p.l) {
              nxt[$ - 1].r = p.r;
            } else {
              nxt ~= p;
            }
          }
          crt = nxt;
        }
        dp[i] = crt;
      }
      debug {
        foreach (i; 0 .. Q) {
          writeln(i, ": ", dp[i]);
        }
      }
      
      if (!dp[0].empty) {
        writeln("YES");
        int x = dp[0][0].l;
        writeln(x);
        foreach (i; 0 .. Q) {
          foreach (y; x - 1 .. x + 1 + 1) {
            if (y != A[i] && y != B[i] && dp[i + 1].any!(p => (p.l <= y && y <= p.r))) {
              x = y;
              writeln(x);
              goto found;
            }
          }
          assert(false);
         found:
        }
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}
0