結果

問題 No.78 クジ付きアイスバー
ユーザー chocorusk
提出日時 2020-10-03 11:20:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 124 ms / 5,000 ms
コード長 931 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 77,804 KB
実行使用メモリ 54,292 KB
最終ジャッジ日時 2024-07-18 04:13:39
合計ジャッジ時間 7,946 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Integer.parseInt(scanner.next());
		int k=Integer.parseInt(scanner.next());
		String s=scanner.next();
		int x=0, y=0;
		int i;
		for(i=0; i<n; i++) {
			int c=s.charAt(i)-'0';
			if(y==0) {
				x++;
			}else {
				y--;
			}
			y+=c;
			if(i+1>=k) {
				out.println(x);
				out.close();
				scanner.close();
				return;
			}
		}
		if(x<=y) {
			out.println(x);
		}else {
			int ans=x+(k-n)/n*(x-y);
			k%=n;
			if(k>0) {
				int y0=y;
				x=0;
				y=0;
				for(i=0; i<n; i++) {
					int c=s.charAt(i)-'0';
					if(y==0) {
						x++;
					}else {
						y--;
					}
					y+=c;
					if(i+1>=k) {
						ans+=Math.max(0,  x-y0);
						break;
					}
				}
			}
			out.println(ans);
		}
		out.close();
		scanner.close();
	}
}
0