結果

問題 No.1433 Two color sequence
ユーザー tnakao0123tnakao0123
提出日時 2021-03-19 23:33:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 2,574 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 95,924 KB
実行使用メモリ 9,592 KB
最終ジャッジ日時 2023-08-12 04:56:17
合計ジャッジ時間 4,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,552 KB
testcase_01 AC 3 ms
7,592 KB
testcase_02 AC 3 ms
7,700 KB
testcase_03 AC 88 ms
9,360 KB
testcase_04 AC 83 ms
9,404 KB
testcase_05 AC 4 ms
7,656 KB
testcase_06 AC 3 ms
7,876 KB
testcase_07 AC 3 ms
7,608 KB
testcase_08 AC 104 ms
9,460 KB
testcase_09 AC 99 ms
9,420 KB
testcase_10 AC 98 ms
9,248 KB
testcase_11 AC 99 ms
9,244 KB
testcase_12 AC 102 ms
9,384 KB
testcase_13 AC 96 ms
9,300 KB
testcase_14 AC 100 ms
9,308 KB
testcase_15 AC 104 ms
9,448 KB
testcase_16 AC 100 ms
9,388 KB
testcase_17 AC 87 ms
9,384 KB
testcase_18 AC 87 ms
9,592 KB
testcase_19 AC 85 ms
9,304 KB
testcase_20 AC 99 ms
9,456 KB
testcase_21 AC 95 ms
9,480 KB
testcase_22 AC 92 ms
9,400 KB
testcase_23 AC 91 ms
9,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1433.cc:  No.1433 Two color sequence - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_E2 = 1 << 19; // = 524288
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeMax {
  int e2;
  T nodes[MAX_E2], minf;
  SegTreeMax() {}

  void init(int n, T _minf) {
    minf = _minf;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, minf);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = max(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = max(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T max_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return minf;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = max_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = max_range(r0, r1, k * 2 + 2, im, i1);
    return max(v0, v1);
  }
  T max_range(int r0, int r1) { return max_range(r0, r1, 0, 0, e2); }
};

/* global variables */

char s[MAX_N + 4];
ll ass[MAX_N + 1];
SegTreeMax<ll,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d%s", &n, s);

  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    ass[i + 1] = ass[i] + ai * (s[i] == 'B' ? -1 : 1);
  }

  st.init(n + 1, -LINF);
  for (int i = 0; i <= n; i++) st.seti(i, ass[i]);
  st.setall();
  //for (int i = 0; i <= n; i++) printf("%lld ", st.geti(i)); putchar('\n');

  ll maxsum = 0;
  for (int i = 0; i < n; i++) {
    ll sum = st.max_range(i + 1, n + 1) - ass[i];
    //printf("i=%d, sum=%lld\n", i, sum);
    if (maxsum < sum) maxsum = sum;
  }

  for (int i = 0; i <= n; i++) st.seti(i, -ass[i]);
  st.setall();
  //for (int i = 0; i <= n; i++) printf("%lld ", st.geti(i)); putchar('\n');

  for (int i = 0; i < n; i++) {
    ll sum = st.max_range(i + 1, n + 1) - (-ass[i]);
    //printf("i=%d, sum=%lld\n", i, sum);
    if (maxsum < sum) maxsum = sum;
  }

  printf("%lld\n", maxsum);
  return 0;
}
0