結果

問題 No.154 市バス
ユーザー tubo28tubo28
提出日時 2016-06-19 17:02:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,389 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 75,992 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-21 09:42:32
合計ジャッジ時間 1,939 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <string>
using namespace std;

bool solve(string s) {
    int n = s.size();
    int W = count(s.begin(), s.end(), 'W');
    int G = count(s.begin(), s.end(), 'G');
    int R = count(s.begin(), s.end(), 'R');
    if (W == 0 || G == 0 || R == 0 || G != R || s[0] != 'W' || s[n - 1] != 'R') return false;
    vector<int> used(n);
    int w = 0;
    int g = n - 2;
    int r = n - 1;
    while (1) {
        while (r >= 0 && (used[r] || s[r] != 'R')) --r;
        if (r < 0) break;
        g = r - 1;
        while (g >= 0 && (used[g] || s[g] != 'G')) --g;
        if (g < 0) break;
        used[r] = used[g] = true;
        bool found = false;
        while (!found && w < g) {
            if (!used[w] && s[w] == 'W') {
                found = true;
                used[w] = true;
            }
            ++w;
        }
        if (!found) break;
        --r;
    }

    g = n - 1;
    while (g >= 0 && s[g] != 'G') --g;
    w = n - 1;
    while (w >= 0 && s[w] != 'W') --w;
    if (w >= g) return false;
    for (int i = 0; i < n; ++i) {
        if (s[i] != 'W' && !used[i]) return false;
    }
    return true;
}


int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        string s;
        cin >> s;
        puts(solve(s) ? "possible" : "impossible");
    }
}
0