結果

問題 No.1924 3 color painting on a line
ユーザー tentententen
提出日時 2022-05-10 20:35:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,760 bytes
コンパイル時間 5,099 ms
コンパイル使用メモリ 74,580 KB
実行使用メモリ 54,716 KB
最終ジャッジ日時 2023-09-25 03:52:07
合計ジャッジ時間 9,112 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,268 KB
testcase_01 AC 43 ms
49,400 KB
testcase_02 AC 42 ms
49,372 KB
testcase_03 AC 94 ms
52,816 KB
testcase_04 AC 97 ms
51,752 KB
testcase_05 AC 97 ms
52,256 KB
testcase_06 AC 101 ms
52,544 KB
testcase_07 AC 101 ms
51,760 KB
testcase_08 AC 96 ms
52,036 KB
testcase_09 AC 97 ms
52,572 KB
testcase_10 AC 96 ms
52,272 KB
testcase_11 AC 104 ms
52,124 KB
testcase_12 AC 96 ms
52,592 KB
testcase_13 AC 97 ms
52,508 KB
testcase_14 AC 96 ms
51,888 KB
testcase_15 AC 95 ms
51,928 KB
testcase_16 AC 95 ms
51,720 KB
testcase_17 AC 95 ms
52,468 KB
testcase_18 AC 102 ms
52,300 KB
testcase_19 AC 97 ms
51,784 KB
testcase_20 AC 98 ms
52,280 KB
testcase_21 AC 97 ms
52,520 KB
testcase_22 AC 99 ms
52,452 KB
testcase_23 AC 65 ms
50,436 KB
testcase_24 AC 97 ms
52,968 KB
testcase_25 AC 89 ms
52,508 KB
testcase_26 AC 55 ms
50,160 KB
testcase_27 AC 82 ms
52,148 KB
testcase_28 AC 66 ms
50,380 KB
testcase_29 AC 62 ms
50,696 KB
testcase_30 AC 63 ms
50,700 KB
testcase_31 AC 63 ms
50,584 KB
testcase_32 AC 85 ms
51,772 KB
testcase_33 AC 81 ms
51,908 KB
testcase_34 AC 95 ms
52,716 KB
testcase_35 AC 92 ms
52,308 KB
testcase_36 AC 69 ms
50,348 KB
testcase_37 AC 82 ms
51,980 KB
testcase_38 AC 81 ms
51,472 KB
testcase_39 AC 60 ms
50,520 KB
testcase_40 AC 45 ms
49,292 KB
testcase_41 AC 69 ms
50,332 KB
testcase_42 AC 94 ms
54,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        char[] values = sc.next().toCharArray();
        ArrayDeque<Character> deq = new ArrayDeque<>();
        deq.add(values[0]);
        int ans = 0;
        for (int i = 1; i < n; i++) {
            if (values[i] == deq.peekLast()) {
                continue;
            }
            if (deq.size() == 1) {
                deq.addLast(values[i]);
                continue;
            }
            char tmp = deq.pollLast();
            if (values[i] == deq.peekLast()) {
                ans++;
                continue;
            }
            deq.addLast(tmp);
            deq.addLast(values[i]);
        }
        if (deq.size() > 0) {
            char first = deq.pollFirst();
            for (char c : deq) {
                if (first != c) {
                    ans++;
                }
            }
            ans++;
        }
        System.out.println(ans);
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0