結果
| 問題 | 
                            No.2042 RGB Caps
                             | 
                    
| コンテスト | |
| ユーザー | 
                             judgelaw
                         | 
                    
| 提出日時 | 2022-08-19 22:47:46 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,024 bytes | 
| コンパイル時間 | 298 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 50,896 KB | 
| 最終ジャッジ日時 | 2024-10-08 09:58:00 | 
| 合計ジャッジ時間 | 9,674 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | WA * 2 | 
| other | WA * 16 | 
ソースコード
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import defaultdict as dd
# aまでcが最多となる条件を考える
# ⇒c>a//3は必要
# ⇒c>a//2あれば十分
# 必要条件をチェックして大丈夫そうなら適当に構成する
# indexをRGBに変換
i2c=["R","G","B"]
# R,G,Bをindexに変換
c2i=dd(int)
c2i["R"]=0
c2i["G"]=1
c2i["B"]=2
N,K=map(int,input().split())
AC=[]
for _ in range(K):
    a,c=input().split()
    a=int(a)
    AC.append((a,c))
AC=sorted(AC)
# 必要条件チェック
RGB=[0,0,0]
for a,c in AC:
    # 今確認したい色以外の必要数をチェック
    othermax=0
    for i in range(3):
        if c2i[c]==i:continue
        othermax=max(othermax,RGB[i])
    # cはc>a//3かそれまでの最多数を更新する必要がある
    RGB[c2i[c]]=max((a+2)//3,othermax,0)
    # RGBの総和がaを上回ればNG
    if a<sum(RGB):
        print(-1)
        exit(0)
# 達成可能ならば適当に構成する
res=[]
i=0
resRGB=[0,0,0]
for a,c in AC:
    
    # まずはaまで目的の色が最多となるようにする
    othermax=0
    for i in range(3):
        if c2i[c]==i:continue
        othermax=max(othermax,resRGB[i])
    target=max((a+2)//3,othermax,0)
    while target-resRGB[i]>0:
        res.append(c)
        resRGB[c2i[c]]+=1
        RGB[c2i[c]]-=1
        target-=1
    # 他の色が残っている場合は最多条件を破らないようにいい感じに埋める
    for i in range(3):
        if c2i[c]==i:continue
        while RGB[i] and resRGB[i]<resRGB[c2i[c]] and len(res)<a:
            res.append(i2c[i])
            resRGB[i]+=1
            RGB[i]-=1
    for i in range(3):
        if c2i[c]==i:continue
        while resRGB[i]<resRGB[c2i[c]] and len(res)<a:
            res.append(i2c[i])
            resRGB[i]+=1
            RGB[i]-=1
    print(a,c,target,resRGB)
#全クエリを見ても残っている場合は適当にRを埋める
while len(res)<N:res.append("R")
print(*res,sep="")
            
            
            
        
            
judgelaw