結果

問題 No.154 市バス
ユーザー tubo28tubo28
提出日時 2016-06-19 17:47:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,688 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 76,148 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 11:47:20
合計ジャッジ時間 1,612 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <string>
#include <cassert>
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 || W < R || s[0] != 'W' || s[n - 1] != 'R') {
        return false;
    }
    int cnt = 0;
    int last_w = 0;
    int last_g = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'W') {
            ++cnt;
            if (cnt > R) {
                s[i] = 0;
            }
            last_w = i;
        }
        if (s[i] == 'G') {
            last_g = i;
        }
    }
    if (last_g < last_w) {
        return false;
    }
    s.erase(remove(s.begin(), s.end(), 0), s.end());
    n = s.size();
    W = R;
    assert(W + G + R == n);

    vector<int> used(n);
    int r = n - 1;
    int g = n - 1;
    int w = n - 1;
    while(r >= 0){
        while (r >= 0 && (used[r] || s[r] != 'R')) --r;
        g = min(g - 1, r - 1);
        while (g >= 0 && (used[g] || s[g] != 'G')) --g;
        w = min(w - 1, g - 1);
        while (w >= 0 && (used[w] || s[w] != 'W')) --w;
        if (r < 0 || g < 0 || w < 0 || used[r] || used[g] || used[w]) {
            break;
        }
        used[r] = used[g] = used[w] = true;
    }
    cnt = count(used.begin(), used.end(), true);
    if (cnt != n) {
        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