結果

問題 No.1288 yuki collection
ユーザー simansiman
提出日時 2022-03-26 11:58:36
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,177 bytes
コンパイル時間 1,971 ms
コンパイル使用メモリ 147,700 KB
実行使用メモリ 74,368 KB
最終ジャッジ日時 2024-04-23 04:23:08
合計ジャッジ時間 44,748 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2,031 ms
36,480 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 -
testcase_33 AC 1,963 ms
35,968 KB
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 2010;
const ll INF = LLONG_MAX;

typedef pair<ll, int> P;

struct Edge {
  int to;
  ll cap;
  ll cost;
  int rev;

  Edge(int to = -1, ll cap = -1, ll cost = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->cost = cost;
    this->rev = rev;
  }
};

class MinCostFlow {
  public:
    int V;
    ll h[MAX_V];
    ll dist[MAX_V];
    int prevv[MAX_V];
    int preve[MAX_V];
    vector <Edge> G[MAX_V];

    MinCostFlow(int V) {
      this->V = V;
    }

    void add_edge(int from, int to, ll cap, ll cost) {
      G[from].push_back(Edge(to, cap, cost, G[to].size()));
      G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
    }

    ll min_cost_flow(int s, int t, ll flow_limit) {
      ll f = 0;
      ll totalCost = 0;
      fill(h, h + MAX_V, 0);

      while (f < flow_limit) {
        fprintf(stderr, "f: %lld, limit: %lld\n", f, flow_limit);
        priority_queue<P, vector<P>, greater<P>> pque;
        fill(dist, dist + V, INF);
        dist[s] = 0;
        pque.push(P(0, s));

        while (!pque.empty()) {
          P p = pque.top();
          pque.pop();
          int v = p.second;
          if (dist[v] < p.first) continue;

          for (int i = 0; i < (int) G[v].size(); ++i) {
            Edge *edge = &G[v][i];
            if (edge->cap <= 0) continue;

            ll cost = edge->cost + h[v] - h[edge->to];
            if (dist[edge->to] - dist[v] > cost) {
              dist[edge->to] = dist[v] + cost;
              prevv[edge->to] = v;
              preve[edge->to] = i;
              pque.push(P(dist[edge->to], edge->to));
            }
          }
        }

        if (dist[t] == INF) {
          return -1;
        }

        for (int v = 0; v < V; ++v) {
          h[v] += dist[v];
        }

        ll c = flow_limit - f;
        for (int v = t; v != s; v = prevv[v]) {
          c = min(c, G[prevv[v]][preve[v]].cap);
        }

        f += c;
        totalCost += c * h[t];

        // fprintf(stderr, "h[s]: %d, h[t]: %d, cost: %d, prev_cost: %d\n", h[s], h[t], cost, prev_cost);

        for (int v = t; v != s; v = prevv[v]) {
          Edge *edge = &G[prevv[v]][preve[v]];
          edge->cap -= c;
          G[v][edge->rev].cap += c;
        }
      }

      return totalCost;
    }
};

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> V(N);

  for (int i = 0; i < N; ++i) {
    cin >> V[i];
  }

  map<char, int> counter;
  map<char, int> c_counter;
  int ch_nums[27];
  memset(ch_nums, 0, sizeof(ch_nums));
  for (int i = 0; i < N; ++i) {
    char s = S[i];
    c_counter[s]++;
    ch_nums[i] = c_counter[s];

    if (s == 'y') {
      counter[s]++;
    } else if (s == 'u') {
      counter[s] = min(counter['y'], c_counter[s]);
    } else if (s == 'k') {
      counter[s] = min(counter['u'], c_counter[s]);
    } else if (s == 'i') {
      counter[s] = min(counter['k'], c_counter[s]);
    }
  }

  MinCostFlow mcf(N + 2);
  int from = N;
  int to = N + 1;
  ll GETA = 1000000000;

  for (int i = 0; i < N; ++i) {
    char s = S[i];
    char target;

    for (int j = i + 1; j < N; ++j) {
      if (S[j] == s) {
        mcf.add_edge(i, j, N + 1, 0);
      }
    }

    if (s == 'y') {
      mcf.add_edge(from, i, 1, 0);

      for (int j = i + 1; j < N; ++j) {
        if (S[j] != 'u') continue;

        mcf.add_edge(i, j, 1, GETA - V[i]);
        break;
      }
    } else if (s == 'u') {
      for (int j = i + 1; j < N; ++j) {
        if (S[j] != 'k') continue;

        mcf.add_edge(i, j, 1, GETA - V[i]);
        break;
      }
    } else if (s == 'k') {
      for (int j = i + 1; j < N; ++j) {
        if (S[j] != 'i') continue;

        mcf.add_edge(i, j, 1, GETA - V[i]);
        break;
      }
    } else if (s == 'i') {
      mcf.add_edge(i, to, 1, GETA - V[i]);
    }
  }

  int flow = counter['i'];
  ll res = mcf.min_cost_flow(from, to, flow);
  cout << GETA * flow * 4 - res << endl;

  return 0;
}
0