結果

問題 No.154 市バス
コンテスト
ユーザー koheijkt
提出日時 2026-02-24 17:39:28
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 2,014 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 741 ms
コンパイル使用メモリ 20,828 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2026-02-24 17:39:34
合計ジャッジ時間 5,060 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys, math
sys.setrecursionlimit(10**8)
sys.set_int_max_str_digits(0)
INF = 1e18
MOD = 998244353
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import product, combinations, permutations, groupby, accumulate
from heapq import heapify, heappop, heappush
input = sys.stdin.readline
def I():   return input().rstrip()
def II():  return int(input().rstrip())
def IS():  return input().rstrip().split()
def MII(): return map(int, input().rstrip().split())
def LI():  return list(input().rstrip())
def TII(): return tuple(map(int, input().rstrip().split()))
def LII(): return list(map(int, input().rstrip().split()))
def LSI(): return list(map(str, input().rstrip().split()))
def GMI(): return list(map(lambda x: int(x) - 1, input().rstrip().split()))
def kiriage(a, b): return (a+b-1)//b

T = II()
d = {}
d['W'] = 0
d['G'] = 1
d['R'] = 2

for i in range(T):
    S = I()
    data = [list() for _ in range(3)]
    for i, s in enumerate(S):
        data[d[s]].append(i)

    # 一個はあるはず
    # G, R は同じ個数のはず
    # W のカウントは G より多いはず
    if len(data[0]) == 0 or len(data[1]) == 0 or len(data[2]) == 0 or len(data[1]) != len(data[2]) or len(data[0]) < len(data[1]):
        print('impossible')
        continue
    # 一番右にある G をメモ
    rightest_G = data[1][-1]
    flag = True
    while data[2]:
        # R
        pos2 = data[2].pop()
        # G: 貪欲に一番後ろを取る
        pos1 = data[1].pop()
        # W: 貪欲に一番後ろを取る
        pos0 = data[0].pop()
        # 順番の判定
        if not (pos0 < pos1 < pos2):
            flag = False
            break
        # break しなかったら次のペアへ

    # W が残っている場合、最右のG より 全部左側にあるべき
    if len(data[0]) > 0:
        if data[0][-1] > rightest_G:
            flag = False

    if flag:
        print('possible')
    else:
        print('impossible')

0