結果

問題 No.805 UMG
ユーザー ut0sut0s
提出日時 2019-09-13 22:20:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,567 bytes
コンパイル時間 1,682 ms
コンパイル使用メモリ 168,100 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 19:41:54
合計ジャッジ時間 2,810 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/**
  @file 805.cpp
  @title  No.805 UMG - yukicoder
  @url https://yukicoder.me/problems/no/805
**/

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, N) for (int i = 0; i < (N); ++i)

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  vector<int> m;
  vector<int> dp_U(N + 1, 0);
  vector<int> dp_G(N + 1, 0);
  REP(i, N) {
    if (S.substr(i, 1) == "M") {
      m.push_back(i);
    }

    if (S.substr(i, 1) == "U") {
      dp_U[i + 1] = dp_U[i] + 1;
    } else {
      dp_U[i + 1] = dp_U[i];
    }

    if (S.substr(i, 1) == "G") {
      dp_G[i + 1] = dp_G[i] + 1;
    } else {
      dp_G[i + 1] = dp_G[i];
    }
  }

  int M = (int)m.size();

  LL ans = 0;
  if (M != 0) {
    REP(i, M) {
      int numU = dp_U[m[i]] - dp_U[0];
      int numG = dp_G[N] - dp_G[m[i]];
      if (numU <= numG) {
        for (int j = 0; j < m[i]; j++) {
          // cout << "j\t" << j << " S[j]:\t" << S[j] << "\n";
          // cout << "m[i]+(m[i]-j)\t" << m[i] + (m[i] - j) << " S[]:\t" << S[j] << "\n";
          if (S[j] == 'U' && S[m[i] + (m[i] - j)] == 'G') {
            ans++;
          }
        }
      } else {
        for (int j = m[i] + 1; j < N; j++) {
          // cout << "j\t" << j << " S[j]:\t" << S[j] << "\n";
          // cout << "m[i]-(j-m[i])\t" << m[i] - (j - m[i]) << " S[]:\t" << S[m[i] - (j - m[i])] << "\n";
          if (S[j] == 'G' && S[m[i] - (j - m[i])] == 'U') {
            ans++;
          }
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}
0