結果

問題 No.2042 RGB Caps
ユーザー judgelawjudgelaw
提出日時 2022-08-19 22:47:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,024 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 51,176 KB
最終ジャッジ日時 2024-04-17 01:25:08
合計ジャッジ時間 9,299 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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="")
0