結果

問題 No.3114 0→1
ユーザー Sillpherth
提出日時 2025-04-20 16:45:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 1,457 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 80,788 KB
実行使用メモリ 58,212 KB
最終ジャッジ日時 2025-04-20 16:45:44
合計ジャッジ時間 10,803 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	public static void out(boolean[] A, int k)
	{
	    for(int i=k-1; i<A.length; i++)
		{
		    System.out.print(A[i]?1:0);
		}
		System.out.println();
	}
	
	public static void toBitarray(String s, boolean[] S, int k, int fullN)
	{
		for(int i=0; i<k-1; i++)
		{
		    S[i] = true;
		}
		
		for(int i=k-1; i<fullN; i++)
		{
		    S[i] = (s.charAt(i-k+1) == '1')?true:false;
		}
	}
	
	public static int min_find(String s, int N, int n)
	{
		final int weight_min = (n+1)/2;
		final int fullN = N+weight_min-1;
		int change = 0;
		int H_weight = 0;
		boolean[] S = new boolean[fullN];
		boolean[] K = new boolean[fullN];
		
		toBitarray(s, S, weight_min, fullN);
		
		System.arraycopy(S, 0, K, 0, fullN);
		//out(S, weight_min);
		
		for(int i=0; i<n; i++)
		{
		    H_weight = H_weight + (S[i]?1:0);
		}
		
		if(H_weight < weight_min)
		{
		    S[n-1] = true;
		    H_weight++;
		    change++;
		}
		    
		for(int i=1; i<fullN-n+1; i++)
		{
		    H_weight = H_weight-(S[i-1]?1:0);
		    H_weight = H_weight+(S[i+n-1]?1:0);
		    if(H_weight < weight_min)
		    {
		        S[i+n-1] = true;
		        H_weight++;
		        change++;
		    }
		    //out(S);
		}
		//out(S, weight_min);
		return change;
	}
	
	public static void main(String[] args)
	{
	    Scanner scan = new Scanner(System.in);
	    final int N = scan.nextInt();
	    final String s = scan.next();
	    System.out.println(min_find(s, N, 3));
	}
}
0