結果

問題 No.1924 3 color painting on a line
ユーザー tentententen
提出日時 2023-06-29 16:44:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 88,016 KB
実行使用メモリ 54,116 KB
最終ジャッジ日時 2023-09-20 12:01:25
合計ジャッジ時間 9,575 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,504 KB
testcase_01 AC 46 ms
49,544 KB
testcase_02 AC 46 ms
49,512 KB
testcase_03 AC 108 ms
52,840 KB
testcase_04 AC 108 ms
51,952 KB
testcase_05 AC 107 ms
52,612 KB
testcase_06 AC 107 ms
52,648 KB
testcase_07 AC 114 ms
52,116 KB
testcase_08 AC 114 ms
52,428 KB
testcase_09 AC 108 ms
52,396 KB
testcase_10 AC 109 ms
52,304 KB
testcase_11 AC 114 ms
52,440 KB
testcase_12 AC 113 ms
52,452 KB
testcase_13 AC 113 ms
52,276 KB
testcase_14 AC 112 ms
52,420 KB
testcase_15 AC 112 ms
52,452 KB
testcase_16 AC 109 ms
53,020 KB
testcase_17 AC 109 ms
52,128 KB
testcase_18 AC 118 ms
54,116 KB
testcase_19 AC 108 ms
52,308 KB
testcase_20 AC 115 ms
52,252 KB
testcase_21 AC 114 ms
52,584 KB
testcase_22 AC 113 ms
53,088 KB
testcase_23 AC 73 ms
50,968 KB
testcase_24 AC 113 ms
52,324 KB
testcase_25 AC 104 ms
52,340 KB
testcase_26 AC 59 ms
50,968 KB
testcase_27 AC 87 ms
51,544 KB
testcase_28 AC 73 ms
50,456 KB
testcase_29 AC 68 ms
50,404 KB
testcase_30 AC 66 ms
50,380 KB
testcase_31 AC 66 ms
50,700 KB
testcase_32 AC 103 ms
51,864 KB
testcase_33 AC 91 ms
50,988 KB
testcase_34 AC 108 ms
52,232 KB
testcase_35 AC 103 ms
51,948 KB
testcase_36 AC 77 ms
50,708 KB
testcase_37 AC 87 ms
52,280 KB
testcase_38 AC 89 ms
52,460 KB
testcase_39 AC 59 ms
50,672 KB
testcase_40 AC 46 ms
49,736 KB
testcase_41 AC 75 ms
48,600 KB
testcase_42 AC 112 ms
52,460 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