結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー 👑 emthrmemthrm
提出日時 2021-06-06 19:51:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,407 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 214,072 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-15 09:11:53
合計ジャッジ時間 15,660 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
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 AC 1 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {}
  };

  std::vector<std::vector<Edge>> graph;

  Dinic(int n) : graph(n), level(n), itr(n) {}

  void add_edge(int src, int dst, T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(int s, int t, T limit) {
    T res = 0;
    while (true) {
      std::fill(level.begin(), level.end(), -1);
      std::queue<int> que;
      level[s] = 0;
      que.emplace(s);
      while (!que.empty()) {
        int ver = que.front(); que.pop();
        for (const Edge &e : graph[ver]) {
          if (level[e.dst] == -1 && e.cap > 0) {
            level[e.dst] = level[ver] + 1;
            que.emplace(e.dst);
          }
        }
      }
      if (level[t] == -1) return res;
      std::fill(itr.begin(), itr.end(), 0);
      T f;
      while ((f = dfs(s, t, limit)) > 0) res += f;
    }
  }

private:
  std::vector<int> level, itr;

  T dfs(int ver, int t, T flow) {
    if (ver == t) return flow;
    for (; itr[ver] < graph[ver].size(); ++itr[ver]) {
      Edge &e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        T tmp = dfs(e.dst, t, std::min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

constexpr int N = 3;
const string PON = "pon";

int solve(const string &s) {
  int l = s.length();
  vector<int> pon[N]{};
  REP(i, l) REP(j, N) {
    if (s[i] == PON[j]) pon[j].emplace_back(i);
  }
  REP(j, N) {
    if (pon[j].empty()) return 0;
  }
  Dinic<int> dinic(l + 2);
  const int src = l, t = l + 1;
  dinic.add_edge(src, pon[0].front(), l);
  REP(j, N) {
    FOR(i, 1, pon[j].size()) dinic.add_edge(pon[j][i - 1], pon[j][i], l);
    if (j + 1 < N) {
      for (int i : pon[j]) {
        auto it = upper_bound(ALL(pon[j + 1]), i);
        if (it != pon[j + 1].end()) dinic.add_edge(i, *it, 1);
      }
    } else {
      for (int i : pon[j]) dinic.add_edge(i, t, 1);
    }
  }
  return dinic.maximum_flow(src, t, l);
}

// https://yukicoder.me/problems/no/1288
int main() {
  int n; string s; cin >> n >> s;
  int ans = -1;
  for (int i = 3; i + 3 <= n; ++i) {
    int l = solve(s.substr(0, i)), r = solve(s.substr(i, n));
    if (l > 0 && r > 0) chmax(ans, l + r - 2);
  }
  cout << ans << '\n';
  return 0;
}
0