結果

問題 No.1029 JJOOII 3
ユーザー SSRSSSRS
提出日時 2020-04-17 22:59:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,248 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 170,456 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 15:20:26
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,944 KB
testcase_02 WA -
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 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
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 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 RE -
testcase_40 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000;
int main(){
  int N, K;
  cin >> N >> K;
  vector<string> S(N);
  vector<long long> C(N);
  for (int i = 0; i < N; i++){
    cin >> S[i] >> C[i];
  }
  vector<int> J(N);
  vector<int> O(N);
  vector<int> I(N);
  bool Jflg = false;
  bool Oflg = false;
  bool Iflg = false;
  for (int i = 0; i < N; i++){
    for (char c : S[i]){
      if (c == 'J'){
        J[i]++;
        Jflg = true;
      }
      if (c == 'O'){
        O[i]++;
        Oflg = true;
      }
      if (c == 'I'){
        I[i]++;
        Iflg = true;
      }
    }
  }
  if (!(Jflg && Oflg && Iflg)){
    cout << -1 << endl;
  } else {
    vector<long long> dpJ(K + 1, INF);
    dpJ[0] = 0;
    for (int i = 0; i < N; i++){
      for (int j = 0; j < J[i]; j++){
        dpJ[j] = min(dpJ[j], C[i]);
      }
      for (int j = J[i]; j <= K; j++){
        dpJ[j] = min(dpJ[j], dpJ[j - J[i]] + C[i]);
      }
    }
    vector<long long> dpO(K + 1, INF);
    dpO[0] = 0;
    for (int i = 0; i < N; i++){
      for (int j = 0; j < O[i]; j++){
        dpO[j] = min(dpO[j], C[i]);
      }
      for (int j = O[i]; j <= K; j++){
        dpO[j] = min(dpO[j], dpO[j - O[i]] + C[i]);
      }
    }
    vector<long long> dpI(K + 1, INF);
    dpI[0] = 0;
    for (int i = 0; i < N; i++){
      for (int j = 0; j < I[i]; j++){
        dpI[j] = min(dpI[j], C[i]);
      }
      for (int j = I[i]; j <= K; j++){
        dpI[j] = min(dpI[j], dpI[j - I[i]] + C[i]);
      }
    }
    /*
    vector<vector<int>> Jcnt(N);
    //Jで左から累積和
    for (int i = 0; i < N; i++){
      Jcnt[i] = vector<int>(S[i].size() + 1, 0);
      for (int j = 0; j < S[i].size(); j++){
        if (S[i][j] == 'J'){
          Jcnt[i][j + 1] = Jcnt[i][j] + 1;
        } else {
          Jcnt[i][j + 1] = Jcnt[i][j];
        }
      }
    }
    vector<vector<int>> Ocnt1(N);
    //Oで左から累積和
    for (int i = 0; i < N; i++){
      Ocnt1[i] = vector<int>(S[i].size() + 1, 0);
      for (int j = 0; j < S[i].size(); j++){
        if (S[i][j] == 'O'){
          Ocnt1[i][j + 1] = Ocnt1[i][j] + 1;
        } else {
          Ocnt1[i][j + 1] = Ocnt1[i][j];
        }
      }
    }
    vector<vector<int>> Ocnt2(N);
    //Oで右から累積和
    for (int i = 0; i < N; i++){
      Ocnt2[i] = vector<int>(S[i].size() + 1, 0);
      for (int j = S[i].size() - 1; j >= 0; j--){
        if (S[i][j] == 'O'){
          Ocnt2[i][j] = Ocnt2[i][j + 1] + 1;
        } else {
          Ocnt2[i][j] = Ocnt2[i][j + 1];
        }
      }
    }
    vector<vector<int>> Icnt(N);
    //Iで右から累積和
    for (int i = 0; i < N; i++){
      Icnt[i] = vector<int>(S[i].size() + 1, 0);
      for (int j = S[i].size() - 1; j >= 0; j--){
        if (S[i][j] == 'I'){
          Icnt[i][j] = Icnt[i][j + 1] + 1;
        } else {
          Icnt[i][j] = Icnt[i][j + 1];
        }
      }
    }
    */
    long long ans = INF;
    /*
    //A: J->O->I
    for (int i = 0; i < N; i++){
      if (O[i] >= K){
        vector<int> Opos;
        for (int j = 0; j < S[i].size(); j++){
          if (S[i][j] == 'O'){
            Opos.push_back(j);
          }
        }
        for (int j = 0; j <= O[i] - K; j++){
          int Jcurr = Jcnt[i][Opos[j]];
          int Icurr = Icnt[i][Opos[j + K - 1] + 1];
          long long cost = C[i];
          if (Jcurr < K){
            cost += dpJ[K - Jcurr];
          }
          if (Icurr < K){
            cost += dpI[K - Icurr];
          }
          ans = min(ans, cost);
        }
      }
    }
    //B: J->O, O->I
    for (int i = 0; i < N; i++){
      for (int j = 0; j < N; j++){
        for (int k = 0; k < S[i].size() + 1; k++){
          for (int l = 0; l < S[j].size() + 1; l++){
            int Jcurr = Jcnt[i][k];
            int Ocurr = Ocnt2[i][k] + Ocnt1[j][l];
            int Icurr = Icnt[j][l];
            long long cost = C[i] + C[j];
            if (Jcurr < K){
              cost += dpJ[K - Jcurr];
            }
            if (Ocurr < K){
              cost += dpO[K - Ocurr];
            }
            if (Icurr < K){
              cost += dpI[K - Icurr];
            }
            ans = min(ans, cost);
          }
        }
      }
    }
    */
    cout << ans << endl;
  }
}
0