結果

問題 No.1288 yuki collection
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-11-13 21:47:17
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 5,172 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 108,296 KB
実行使用メモリ 8,304 KB
最終ジャッジ日時 2023-09-04 10:59:24
合計ジャッジ時間 48,398 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 4 ms
4,500 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 3,231 ms
4,384 KB
testcase_14 AC 3,979 ms
4,380 KB
testcase_15 AC 2,978 ms
4,384 KB
testcase_16 AC 2,205 ms
4,380 KB
testcase_17 AC 4,787 ms
4,376 KB
testcase_18 AC 3,923 ms
4,376 KB
testcase_19 AC 2,726 ms
4,892 KB
testcase_20 AC 4,679 ms
4,380 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

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


class MinCostFlow(wint, cint) {
  int n, m;
  int[][] g;
  int[] zu;
  wint[] capa0, capa, ex;
  cint[] cost;
  real[] pot;
  bool[] vis;
  int[] itr, lev;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; zu = null; capa = null; cost = null;
    ex = new wint[n]; pot = new real[n]; pot[] = 0; vis = new bool[n]; itr = new int[n]; lev = new int[n];
  }
  void addEdge(int u, int v, wint w, cint c) {
    g[u] ~= m; zu ~= v; capa0 ~= w; capa ~= w; cost ~= +c; ++m;
    g[v] ~= m; zu ~= u; capa0 ~= 0; capa ~= 0; cost ~= -c; ++m;
  }
  wint augment(int u, int t, wint f) {
    if (u == t) return f;
    foreach (i; g[u][itr[u] .. $]) {
      if (capa[i] > 0 && lev[u] < lev[zu[i]]) {
        wint g = augment(zu[i], t, min(f, capa[i]));
        if (g > 0) {
          capa[i] -= g; capa[i ^ 1] += g; return g;
        }
      }
      ++itr[u];
    }
    return 0;
  }
  wint augment(int u, wint f) {
    if (ex[u] < 0) {
      wint g = min(f, -ex[u]); ex[u] += g; return g;
    }
    foreach (i; g[u][itr[u] .. $]) {
      if (capa[i] > 0 && cost[i] + pot[u] - pot[zu[i]] < 0) {
        wint g = augment(zu[i], min(f, capa[i]));
        if (g > 0) {
          capa[i] -= g; capa[i ^ 1] += g; return g;
        }
      }
      ++itr[u];
    }
    return 0;
  }
  wint dinic(int s, int t, wint f) {
    wint tof = 0;
    for (; tof < f; ) {
      int[] q;
      lev[] = -1;
      for (lev[s] = 0, q ~= s; !q.empty; ) {
        int u = q.front; q.popFront;
        foreach (i; g[u]) {
          int v = zu[i];
          if (capa[i] > 0 && lev[v] == -1) {
            lev[v] = lev[u] + 1; q ~= v;
          }
        }
      }
      if (lev[t] == -1) break;
      itr[] = 0;
      for (wint g; (g = augment(s, t, f - tof)) > 0; ) tof += g;
    }
    return tof;
  }
  void dfs(int u) {
    if (vis[u]) return; vis[u] = true;
    foreach (i; g[u]) if (capa[i] > 0 && cost[i] + pot[u] - pot[zu[i]] < 0) {
      dfs(zu[i]);
    }
  }
  cint solve() {
    real eps = 0;
  // cint solve(int s, int t, wint flow) {
    // real eps = 1;
    // ex[s] += flow;
    // ex[t] -= flow;
    foreach (i; 0 .. m) if (capa[i] > 0) chmax(eps, cast(real)(-cost[i]));
    for (; eps * n >= 1; ) {
      eps /= 4;
      foreach (i; 0 .. m) if (capa[i] > 0 && cost[i] + pot[zu[i ^ 1]] - pot[zu[i]] < 0) {
        ex[zu[i ^ 1]] -= capa[i]; ex[zu[i]] += capa[i]; capa[i ^ 1] += capa[i]; capa[i] = 0;
      }
      for (; ; ) {
        vis[] = false; itr[] = 0;
        foreach (u; 0 .. n) if (ex[u] > 0) dfs(u);
        foreach (u; 0 .. n) if (vis[u]) pot[u] -= eps;
        foreach (u; 0 .. n) for (wint f; ex[u] > 0 && (f = augment(u, ex[u])) > 0; ) ex[u] -= f;
        if (ex.all!"a <= 0") break;
      }
    }
    cint toc = 0;
    foreach (i; 0 .. m) if (capa0[i] > capa[i]) toc += (capa0[i] - capa[i]) * cost[i];
    return toc;
  }
}


void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const S = readToken();
      auto V = new long[N];
      foreach (i; 0 .. N) {
        V[i] = readLong();
      }
      
      auto mcf = new MinCostFlow!(int, long)(N * 2 + 2);
      mcf.addEdge(N * 2 + 1, N * 2, N, 0);
      foreach (i; 0 .. N) {
        if (S[i] == 'y') {
          mcf.addEdge(N * 2, i * 2, N, 0);
        }
        if (S[i] == 'i') {
          mcf.addEdge(i * 2 + 1, N * 2 + 1, N, 0);
        }
        mcf.addEdge(i * 2, i * 2 + 1, 1, -V[i]);
        foreach (j; i + 1 .. N) {
          if (S[i] == S[j]) {
            debug {
              writeln("? ", i, " ", j);
            }
            mcf.addEdge(i * 2, j * 2, N, 0);
            break;
          }
        }
        foreach (j; i + 1 .. N) {
          if ((S[i] == 'y' && S[j] == 'u') || (S[i] == 'u' && S[j] == 'k') || (S[i] == 'k' && S[j] == 'i')) {
            debug {
              writeln("! ", i, " ", j);
            }
            mcf.addEdge(i * 2 + 1, j * 2, N, 0);
            break;
          }
        }
      }
      
      const res = mcf.solve();
      writeln(-res);
    }
  } catch (EOFException e) {
  }
}
0