結果

問題 No.576 E869120 and Rings
ユーザー 37zigen37zigen
提出日時 2017-10-14 23:21:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,380 ms / 1,500 ms
コード長 3,764 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 79,044 KB
実行使用メモリ 98,476 KB
最終ジャッジ日時 2024-06-11 15:41:44
合計ジャッジ時間 27,930 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,208 ms
69,060 KB
testcase_01 AC 1,233 ms
69,076 KB
testcase_02 AC 1,337 ms
69,032 KB
testcase_03 AC 1,380 ms
69,140 KB
testcase_04 AC 1,305 ms
68,952 KB
testcase_05 AC 1,194 ms
69,056 KB
testcase_06 AC 1,178 ms
69,092 KB
testcase_07 AC 1,333 ms
68,936 KB
testcase_08 AC 910 ms
68,444 KB
testcase_09 AC 974 ms
70,620 KB
testcase_10 AC 937 ms
68,376 KB
testcase_11 AC 816 ms
68,892 KB
testcase_12 AC 835 ms
68,396 KB
testcase_13 AC 855 ms
68,568 KB
testcase_14 AC 898 ms
68,440 KB
testcase_15 AC 341 ms
65,036 KB
testcase_16 AC 1,178 ms
98,476 KB
testcase_17 AC 827 ms
68,792 KB
testcase_18 AC 817 ms
69,360 KB
testcase_19 AC 797 ms
68,948 KB
testcase_20 AC 743 ms
68,928 KB
testcase_21 AC 830 ms
69,000 KB
testcase_22 AC 53 ms
49,948 KB
testcase_23 AC 48 ms
50,020 KB
testcase_24 AC 47 ms
50,140 KB
testcase_25 AC 46 ms
49,932 KB
testcase_26 AC 47 ms
50,040 KB
testcase_27 AC 48 ms
50,056 KB
testcase_28 AC 47 ms
49,956 KB
testcase_29 AC 48 ms
49,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}

	boolean check(double m, int n, int k, int[] a) {
		double[] sum = new double[2 * n];
		for (int i = 0; i < 2 * n; ++i) {
			if (a[i % n] == 1)
				sum[i] += 1 - m;
			else
				sum[i] -= m;
			sum[i] += i > 0 ? sum[i - 1] : 0;
		}
		if (sum[k - 1] >= 0)
			return true;
		MonotonePriorityQueue que = new MonotonePriorityQueue();
		for (int i = 0; i < k; ++i) {
			que.add(sum[i]);
		}
		for (int i = k; i + k < 2 * n; ++i) {
			que.add(sum[i]);
			if (que.sz > n - k + 1)
				que.poll();
			if (sum[i + k] - que.min() >= 0)
				return true;
		}
		return false;
	}

	void run() {
		Scanner sc = new Scanner();
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] a = new int[n];
		String str = sc.next();
		for (int i = 0; i < n; ++i)
			a[i] = str.charAt(i) - '0';
		double ng = 1;
		double ok = 0;
		for (int t = 0; t < 30; ++t) {
			double middle = (ok + ng) / 2;
			if (check(middle, n, k, a)) {
				ok = middle;
			} else {
				ng = middle;
			}
		}
		System.out.println(ok);
	}

	class MonotonePriorityQueue {
		int sz;
		ArrayDeque<Val> que = new ArrayDeque<>();

		void poll() {
			if (sz == 0)
				throw new AssertionError();
			--sz;
			--que.getFirst().sz;
			if (que.getFirst().sz == 0)
				que.pollFirst();
		}

		double min() {
			return que.getFirst().val;
		}

		void add(double v) {
			++sz;
			int cnt = 1;
			while (!que.isEmpty() && que.peekLast().val >= v) {
				cnt += que.peekLast().sz;
				que.pollLast();
			}
			que.addLast(new Val(v, cnt));
		}

		private class Val {
			double val;
			int sz;

			public Val(double val_, int sz_) {
				val = val_;
				sz = sz_;
			}
		}
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0