結果

問題 No.1951 消えたAGCT(2)
ユーザー tnakao0123tnakao0123
提出日時 2022-05-21 15:11:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 3,000 ms
コード長 1,606 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 42,764 KB
実行使用メモリ 5,624 KB
最終ジャッジ日時 2023-10-20 16:16:19
合計ジャッジ時間 2,803 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 11 ms
5,624 KB
testcase_09 AC 12 ms
5,624 KB
testcase_10 AC 12 ms
5,624 KB
testcase_11 AC 12 ms
5,624 KB
testcase_12 AC 8 ms
4,652 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 60 ms
5,472 KB
testcase_15 AC 49 ms
5,028 KB
testcase_16 AC 56 ms
5,140 KB
testcase_17 AC 69 ms
5,624 KB
testcase_18 AC 66 ms
5,624 KB
testcase_19 AC 61 ms
5,624 KB
testcase_20 AC 63 ms
5,624 KB
testcase_21 AC 11 ms
5,624 KB
testcase_22 AC 11 ms
5,624 KB
testcase_23 AC 11 ms
5,624 KB
testcase_24 AC 11 ms
5,624 KB
testcase_25 AC 11 ms
5,624 KB
testcase_26 AC 62 ms
5,624 KB
testcase_27 AC 63 ms
5,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1951.cc:  No.1951 消えたAGCT(2) - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 500000;

enum { A = 'A' - 'A', G = 'G' - 'A', C = 'C' - 'A', T = 'T' - 'A' };

/* typedef */

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }

  int lower_bound(T v) {
    int	k = 1;
    while ((k << 1) <= n) k <<=	1;
    int	x = 0;
    for	(; k > 0; k >>= 1)
      if (x + k <= n && bits[x + k] < v) {
        x += k;
        v -= bits[x];
      }
    return x + 1;
  }
};

/* global variables */

char s[MAX_N + 4];
int cs[26];
BIT<int,MAX_N> bit;

/* subroutines */

inline int prv(int x, int d) { return (x + 26 - d) % 26; }

/* main */

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

  for (int i = 0; i < n; i++) cs[s[i] - 'A']++;

  bit.init(n);
  for (int i = 1; i <= n; i++) bit.add(i, 1);
  
  int cnt = 0, d = 0;
  for (;; cnt++) {
    int c1 = cs[prv(A, d)] + cs[prv(G, d)] + cs[prv(C, d)] + cs[prv(T, d)];
    if (c1 == 0) break;

    int x = bit.lower_bound(c1);
    bit.add(x, -1);
    int c2 = --cs[s[x - 1] - 'A'];

    d = (d + c2) % 26;
    //printf("c1=%d, c2=%d, x=%d, d=%d\n", c1, c2, x, d);
  }

  printf("%d\n", cnt);
  return 0;
}
0