結果

問題 No.154 市バス
ユーザー imulanimulan
提出日時 2016-06-20 15:06:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,124 bytes
コンパイル時間 1,383 ms
コンパイル使用メモリ 164,016 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 11:51:25
合計ジャッジ時間 2,219 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
4,376 KB
testcase_01 AC 30 ms
4,376 KB
testcase_02 AC 30 ms
4,380 KB
testcase_03 AC 26 ms
4,376 KB
testcase_04 AC 29 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 23 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int main()
{
    int T;
    cin >>T;
    rep(times,T)
    {
        string s;
        cin >>s;

        int n=s.size();

        //明らかに不可能な例
        if(n<3)
        {
            printf("impossible\n");
            continue;
        }
        else
        {
            if(s[0]!='W' || s[n-1]!='R')
            {
                printf("impossible\n");
                continue;
            }
        }

        int w=0,g=0,r=0;
        rep(i,n)
        {
            if(s[i]=='W') ++w;
            else if(s[i]=='G') ++g;
            else ++r;
        }

        //この時だけ条件を満たす可能性がある
        if(g==r && w>=g)
        {
            bool valid=true;

            //cg,crは常にcw以下
            //cgがgになったらそれ以降wが来てはいけない
            //crがcgより多くなることはない
            int cw=0,cg=0,cr=0;
            rep(i,n)
            {
                if(s[i]=='W')
                {
                    ++cw;
                    if(cg==g)
                    {
                        valid=false;
                        break;
                    }
                }
                else if(s[i]=='G')
                {
                    ++cg;
                    if(cg>cw)
                    {
                        valid=false;
                        break;
                    }
                }
                else
                {
                    ++cr;
                    if(cr>cg || cr>cw)
                    {
                        valid=false;
                        break;
                    }
                }
            }

            if(valid) printf("possible\n");
            else printf("impossible\n");
        }
        else printf("impossible\n");
    }
    return 0;
}
0