結果
問題 | No.1951 消えたAGCT(2) |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:11:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 342 ms / 3,000 ms |
コード長 | 2,793 bytes |
コンパイル時間 | 151 ms |
コンパイル使用メモリ | 82,492 KB |
実行使用メモリ | 85,492 KB |
最終ジャッジ日時 | 2025-03-20 21:11:22 |
合計ジャッジ時間 | 5,265 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
def main():import sysinput = sys.stdin.read().split()n = int(input[0])s = input[1]# Convert characters to numerical values (A=0, B=1, ..., Z=25)original_chars = [ord(c) - ord('A') for c in s]# Initialize count array for each character (0-25)count = [0] * 26for c in original_chars:count[c] += 1# BIT (Fenwick Tree) 1-based indexingclass FenwickTree:def __init__(self, size):self.n = sizeself.tree = [0] * (self.n + 2) # 1-baseddef update_point(self, idx, delta):while idx <= self.n:self.tree[idx] += deltaidx += idx & -idxdef query_prefix(self, idx):res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn resdef find_kth(self, k):# Find the smallest index where prefix sum >= ksum_ = 0pos = 0max_bit = 1while (max_bit << 1) <= self.n:max_bit <<= 1mask = max_bitwhile mask > 0:next_pos = pos + maskif next_pos > self.n:mask >>= 1continueif sum_ + self.tree[next_pos] < k:sum_ += self.tree[next_pos]pos = next_posmask >>= 1return pos + 1 # 1-basedft = FenwickTree(n)for i in range(1, n+1):ft.update_point(i, 1)total_shift = 0ans = 0while True:current_shift = total_shift % 26# Calculate the target original characters that would now contribute to AGCTtargetA = (0 - current_shift) % 26targetG = (6 - current_shift) % 26targetC = (2 - current_shift) % 26targetT = (19 - current_shift) % 26# Sum the counts of these targetsc1 = count[targetA] + count[targetG] + count[targetC] + count[targetT]if c1 == 0:breakans += 1# Find the k-th element (k = c1)k = c1pos = ft.find_kth(k)# Get the original character at pos (1-based)original_char = original_chars[pos - 1] # since original_chars is 0-based# Update countcount[original_char] -= 1# Update the Fenwick Tree (mark as removed)ft.update_point(pos, -1)# c2 is the new count after deletionc2 = count[original_char]# Update total_shifttotal_shift += c2print(ans)if __name__ == '__main__':main()