結果
問題 |
No.1951 消えたAGCT(2)
|
ユーザー |
![]() |
提出日時 | 2022-05-20 23:00:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 199 ms / 3,000 ms |
コード長 | 1,170 bytes |
コンパイル時間 | 1,593 ms |
コンパイル使用メモリ | 168,912 KB |
実行使用メモリ | 5,940 KB |
最終ジャッジ日時 | 2024-09-20 09:22:36 |
合計ジャッジ時間 | 4,596 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; template <typename T> struct binary_indexed_tree{ int N; vector<T> BIT; binary_indexed_tree(int N): N(N), BIT(N + 1, 0){ } void add(int i, T x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T sum(int L, int R){ return sum(R) - sum(L); } }; int main(){ int n; cin >> n; string s; cin >> s; vector<int> cnt(26, 0); for (int i = 0; i < n; i++){ cnt[s[i] - 'A']++; } binary_indexed_tree<int> BIT(n); for (int i = 0; i < n; i++){ BIT.add(i, 1); } int ans = 0; int sh = 0; while (true){ int c1 = cnt[(26 - sh) % 26] + cnt[(28 - sh) % 26] + cnt[(32 - sh) % 26] + cnt[(45 - sh) % 26]; if (c1 == 0){ break; } int tv = 0, fv = n; while (fv - tv > 1){ int mid = (tv + fv) / 2; if (BIT.sum(mid) < c1){ tv = mid; } else { fv = mid; } } BIT.add(tv, -1); int t = s[tv] - 'A'; cnt[t]--; sh += cnt[t]; sh %= 26; ans++; } cout << ans << endl; }