結果

問題 No.2145 Segment +-
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-12-02 21:53:56
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,155 bytes
コンパイル時間 819 ms
コンパイル使用メモリ 107,328 KB
実行使用メモリ 6,692 KB
最終ジャッジ日時 2023-09-04 19:11:22
合計ジャッジ時間 3,306 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 1 ms
4,380 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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)); }


enum INF = 10^^9;

void main() {
  /*
  debug {{
    enum lim = 20;
    foreach (n; 1 .. lim + 1) {
      DList!int que;
      auto dist = new int[1 << n];
      dist[] = -1;
      foreach (u; 0 .. 1 << n) {
        bool ok = true;
        int now;
        foreach (i; 0 .. n) {
          (u >> i & 1) ? --now : ++now;
          ok = ok && (now >= 0);
        }
        if (ok) {
          dist[u] = 0;
          que ~= u;
        }
      }
      for (; !que.empty; ) {
        const u = que.front;
        que.removeFront;
        foreach (i; 0 .. n + 1) foreach (j; i + 1 .. n + 1) {
          const v = u ^ ((1 << j) - (1 << i));
          if (!~dist[v]) {
            dist[v] = dist[u] + 1;
            que ~= v;
          }
        }
      }
      foreach (u; 0 .. 1 << n) if (dist[u] >= 2) {
        const s = iota(n).map!(i => "+-"[u >> i & 1]).array;
        writeln(dist[u], " ", s);
        assert(dist[u] <= 2);
      }
    }
  }}
  //*/
  
  try {
    for (; ; ) {
      const N = readInt;
      auto S = readToken;
      
      auto dp = new int[3][N + 1];
      foreach (i; 0 .. N + 1) {
        static foreach (s; 0 .. 3) {
          dp[i][s] = -INF;
        }
      }
      dp[0][0] = 0;
      foreach (i; 0 .. N + 1) {
        static foreach (s; 0 .. 3) {
          if (dp[i][s] < 0) {
            dp[i][s] = -INF;
          }
        }
        chmax(dp[i][1], dp[i][0]);
        chmax(dp[i][2], dp[i][1]);
        if (i < N) {
          if (S[i] == '?' || S[i] == '+') {
            chmax(dp[i + 1][0], dp[i][0] + 1);
            chmax(dp[i + 1][1], dp[i][1] - 1);
            chmax(dp[i + 1][2], dp[i][2] + 1);
          }
          if (S[i] == '?' || S[i] == '-') {
            chmax(dp[i + 1][0], dp[i][0] - 1);
            chmax(dp[i + 1][1], dp[i][1] + 1);
            chmax(dp[i + 1][2], dp[i][2] - 1);
          }
        }
      }
      const ans = (dp[N][0] >= 0) ? 0 : (dp[N][2] >= 0) ? 1 : 2;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0