結果

問題 No.1508 Avoid being hit
ユーザー 👑 hos.lyric
提出日時 2021-05-14 23:50:00
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 151 ms / 3,000 ms
コード長 2,931 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 126,000 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2024-06-22 11:18:13
合計ジャッジ時間 6,422 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

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