結果

問題 No.154 市バス
ユーザー atkrymatkrym
提出日時 2016-10-24 01:12:39
言語 Java21
(openjdk 21)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,823 bytes
コンパイル時間 3,069 ms
コンパイル使用メモリ 80,300 KB
実行使用メモリ 64,232 KB
最終ジャッジ日時 2024-04-21 10:00:12
合計ジャッジ時間 7,387 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 559 ms
61,016 KB
testcase_02 AC 562 ms
61,016 KB
testcase_03 AC 549 ms
60,836 KB
testcase_04 AC 562 ms
61,696 KB
testcase_05 AC 136 ms
53,900 KB
testcase_06 AC 135 ms
53,796 KB
testcase_07 AC 519 ms
59,280 KB
testcase_08 AC 137 ms
54,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        for (int i = 0;i < n;i++) {
            if (solve(sc.next())) {
                System.out.println("possible");
            } else {
                System.out.println("impossible");
            }
        }
    }
    
    private static boolean solve(String s) {
        int w = 0;
        List<Integer> g = new ArrayList<>();
        List<Integer> r = new ArrayList<>();
        
        for (int i = s.length()-1;i >= 0;i--) {
            char c = s.charAt(i);
            if (c == 'W') {
                if (g.isEmpty() || r.isEmpty()) {
                    return false;
                }
            }
            if (c == 'G') g.add(i);
            if (c == 'R') r.add(i);
        }
        
        g = new ArrayList<>();
        r = new ArrayList<>();
        
        for (int i = 0;i < s.length();i++) {
            char c = s.charAt(i);
            if (c == 'W') w++;
            if (c == 'G') {
                g.add(i);
                if (w < g.size()) return false;
            }
            if (c == 'R') {
                r.add(i);
                if (w < r.size()) return false;
            }
        }

        if (g.size() != r.size()) return false;
        if (w < g.size()) return false;
        
        for (int i = 0;i < g.size();i++) {
            if (r.get(i) < g.get(i)) {
                return false;
            }
        }
        
        return true;
    }
    
    private static void show(List<Integer> list) {
        String s = "";
        for (int i = 0;i < list.size();i++) {
            s += list.get(i);
            s += " ";
        }
        
        System.out.println(s);
    }
}
0