結果

問題 No.1924 3 color painting on a line
ユーザー tentententen
提出日時 2023-06-29 16:44:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 95,788 KB
実行使用メモリ 39,488 KB
最終ジャッジ日時 2024-07-06 07:20:21
合計ジャッジ時間 7,029 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
37,052 KB
testcase_01 AC 43 ms
36,984 KB
testcase_02 AC 42 ms
36,984 KB
testcase_03 AC 92 ms
39,140 KB
testcase_04 AC 90 ms
39,320 KB
testcase_05 AC 92 ms
39,076 KB
testcase_06 AC 90 ms
39,156 KB
testcase_07 AC 93 ms
39,124 KB
testcase_08 AC 92 ms
39,248 KB
testcase_09 AC 90 ms
39,056 KB
testcase_10 AC 90 ms
39,128 KB
testcase_11 AC 83 ms
39,024 KB
testcase_12 AC 91 ms
39,488 KB
testcase_13 AC 95 ms
39,100 KB
testcase_14 AC 89 ms
39,236 KB
testcase_15 AC 88 ms
39,152 KB
testcase_16 AC 90 ms
39,328 KB
testcase_17 AC 86 ms
39,088 KB
testcase_18 AC 87 ms
39,112 KB
testcase_19 AC 91 ms
39,292 KB
testcase_20 AC 92 ms
38,996 KB
testcase_21 AC 91 ms
39,012 KB
testcase_22 AC 87 ms
39,116 KB
testcase_23 AC 66 ms
37,760 KB
testcase_24 AC 86 ms
38,996 KB
testcase_25 AC 81 ms
38,604 KB
testcase_26 AC 50 ms
37,196 KB
testcase_27 AC 70 ms
37,840 KB
testcase_28 AC 64 ms
37,836 KB
testcase_29 AC 60 ms
37,772 KB
testcase_30 AC 57 ms
37,508 KB
testcase_31 AC 66 ms
37,632 KB
testcase_32 AC 78 ms
38,876 KB
testcase_33 AC 75 ms
38,464 KB
testcase_34 AC 96 ms
39,204 KB
testcase_35 AC 90 ms
38,788 KB
testcase_36 AC 74 ms
37,856 KB
testcase_37 AC 70 ms
38,132 KB
testcase_38 AC 71 ms
37,840 KB
testcase_39 AC 52 ms
36,800 KB
testcase_40 AC 42 ms
36,800 KB
testcase_41 AC 67 ms
37,852 KB
testcase_42 AC 94 ms
39,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<Character> list = new ArrayList<>();
        int ans = 0;
        for (char c : sc.next().toCharArray()) {
            if (list.size() == 0) {
                list.add(c);
            } else if (list.size() == 1) {
                if (list.get(0) != c) {
                    list.add(c);
                }
            } else {
                if (list.get(list.size() - 1) != c) {
                    if (list.get(list.size() - 2) == c) {
                        list.remove(list.size() - 1);
                        ans++;
                    } else {
                        list.add(c);
                    }
                }
            }
        }
        int[] counts = new int[3];
        for (char c : list) {
            if (c == 'R') {
                counts[0]++;
            } else if (c == 'G') {
                counts[1]++;
            } else {
                counts[2]++;
            }
        }
        Arrays.sort(counts);
        ans += counts[0] + counts[1] + 1;
        System.out.println(ans);
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0