結果

問題 No.1924 3 color painting on a line
ユーザー 37zigen37zigen
提出日時 2022-04-24 20:56:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 1,952 bytes
コンパイル時間 4,572 ms
コンパイル使用メモリ 79,032 KB
実行使用メモリ 60,964 KB
最終ジャッジ日時 2023-09-14 15:17:22
合計ジャッジ時間 17,485 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,860 KB
testcase_01 AC 129 ms
55,596 KB
testcase_02 AC 128 ms
55,804 KB
testcase_03 AC 303 ms
60,388 KB
testcase_04 AC 304 ms
60,184 KB
testcase_05 AC 307 ms
60,396 KB
testcase_06 AC 307 ms
60,788 KB
testcase_07 AC 307 ms
60,388 KB
testcase_08 AC 306 ms
60,496 KB
testcase_09 AC 302 ms
60,444 KB
testcase_10 AC 301 ms
60,204 KB
testcase_11 AC 307 ms
60,720 KB
testcase_12 AC 301 ms
59,456 KB
testcase_13 AC 302 ms
57,688 KB
testcase_14 AC 305 ms
60,780 KB
testcase_15 AC 308 ms
60,452 KB
testcase_16 AC 302 ms
60,964 KB
testcase_17 AC 302 ms
60,296 KB
testcase_18 AC 303 ms
60,500 KB
testcase_19 AC 302 ms
60,504 KB
testcase_20 AC 312 ms
60,248 KB
testcase_21 AC 302 ms
60,500 KB
testcase_22 AC 300 ms
60,476 KB
testcase_23 AC 184 ms
55,992 KB
testcase_24 AC 300 ms
58,324 KB
testcase_25 AC 284 ms
59,236 KB
testcase_26 AC 165 ms
55,948 KB
testcase_27 AC 249 ms
56,992 KB
testcase_28 AC 182 ms
55,780 KB
testcase_29 AC 171 ms
55,956 KB
testcase_30 AC 171 ms
55,856 KB
testcase_31 AC 170 ms
55,772 KB
testcase_32 AC 288 ms
59,524 KB
testcase_33 AC 277 ms
57,032 KB
testcase_34 AC 296 ms
60,376 KB
testcase_35 AC 284 ms
58,852 KB
testcase_36 AC 247 ms
56,692 KB
testcase_37 AC 255 ms
56,992 KB
testcase_38 AC 255 ms
57,048 KB
testcase_39 AC 168 ms
55,628 KB
testcase_40 AC 137 ms
55,904 KB
testcase_41 AC 218 ms
56,388 KB
testcase_42 AC 303 ms
60,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

public class Main implements Runnable { //Runnableを実装する
    public static void main(String[] args) {
        new Thread(null, new Main(), "", 16 * 1024 * 1024).start(); //16MBスタックを確保して実行
    }
    
    void verify() {
    	Random rnd=new Random();
    	int N=10;
    	int[] a=new int[N];
    	do {
    		for (int i=0;i<N;++i) a[i]=rnd.nextInt(3);
    		if (exact(a) != solve(a)) {
    			tr(exact(a),solve(a),a);
    			break;
    		} else {
    			tr("ok");
    		}
    	} while (true);
    }
    
    
    int exact(int[] a) {
    	int N=a.length;
    	if (N==0) return 0;
    	int ret=Integer.MAX_VALUE/3;
    	for (int  L=0;L<a.length;++L) {
    		int R=L;
    		while (R < N && a[L]==a[R]) ++R;
    		int[] na=new int[N-(R-L)];
    		System.arraycopy(a, 0, na, 0, L);
    		System.arraycopy(a, R, na, L, N-R);
    		ret=Math.min(ret, exact(na)+1);
    	}
    	return ret;
    }
    
    int solve(int[] a) {
		Stack<Integer> stk=new Stack<>();
		int ans=0;
		for (int i=0, j=0; i<a.length; i=j+1) {
			while (j+1<a.length && a[i]==a[j+1]) ++j;
			int v=a[i];
			stk.add(v);
			if (stk.size() >= 2) {
				int x=stk.pop();
				if (x != stk.peek()) stk.add(x);
			}
			if (stk.size() >= 3) {
				int x=stk.pop();
				int y=stk.pop();
				int z=stk.pop();
				if (x==z) {
					ans+=1;
					stk.add(x);
					continue;
				}
				stk.add(z);
				stk.add(y);
				stk.add(x);
			}
		}
		ans+=stk.size() - (stk.size() - 1)/ 3;
		return ans;
    }

    public void run() {
		Scanner sc=new Scanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		int N=sc.nextInt();
		char[] cs=sc.next().toCharArray();
		int[] a=new int[N];
		Arrays.setAll(a, i->"RGB".indexOf(cs[i]));
		pw.println(solve(a));
		pw.close();
	}	
	
	void tr(Object ... o) {System.out.println(Arrays.deepToString(o));}
}
0