結果

問題 No.2042 RGB Caps
ユーザー roaris
提出日時 2022-08-20 02:25:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,143 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 83,712 KB
最終ジャッジ日時 2024-11-16 02:00:18
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def f1(s):
    if s=='R':
        return 0
    elif s=='G':
        return 1
    else:
        return 2

def f2(x):
    if x==0:
        return 'R'
    elif x==1:
        return 'G'
    else:
        return 'B'

N, K = map(int, input().split())
color = [-1]*(N+1)

for _ in range(K):
    A, c = input().split()
    color[int(A)] = f1(c)

cnt = [0, 0, 0]
ans = []

for i in range(1, N+1):
    if color[i]==-1:
        m = min(cnt)
        i = cnt.index(m)
        cnt[i] += 1
        ans.append(i)
    else:
        d = 5
        x = -1

        for j in range(3):
            cnt[j] += 1
            M = max(cnt)
            m = min(cnt)
        
            if color[i] in [k for k in range(3) if cnt[k]==M]:
                if d>M-m:
                    d = M-m
                    x = j
                
            cnt[j] -= 1
        
        assert(x!=-1)
        cnt[x] += 1
        ans.append(x)

cnt = [0, 0, 0]

for i in range(1, N+1):
    cnt[ans[i-1]] += 1
    M = max(cnt)
    
    if color[i]!=-1:
        assert(color[i] in [k for k in range(3) if cnt[k]==M])

print(''.join(map(f2, ans)))
0