結果

問題 No.576 E869120 and Rings
ユーザー 37zigen37zigen
提出日時 2020-03-19 03:11:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,191 ms / 1,500 ms
コード長 3,764 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 79,460 KB
実行使用メモリ 88,308 KB
最終ジャッジ日時 2024-05-08 03:01:26
合計ジャッジ時間 24,763 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,021 ms
58,424 KB
testcase_01 AC 984 ms
58,236 KB
testcase_02 AC 1,115 ms
58,536 KB
testcase_03 AC 1,191 ms
58,592 KB
testcase_04 AC 1,055 ms
58,432 KB
testcase_05 AC 1,051 ms
58,420 KB
testcase_06 AC 1,043 ms
58,280 KB
testcase_07 AC 1,138 ms
58,656 KB
testcase_08 AC 801 ms
58,472 KB
testcase_09 AC 884 ms
58,352 KB
testcase_10 AC 823 ms
58,744 KB
testcase_11 AC 713 ms
58,480 KB
testcase_12 AC 794 ms
58,736 KB
testcase_13 AC 774 ms
58,332 KB
testcase_14 AC 887 ms
57,532 KB
testcase_15 AC 325 ms
53,020 KB
testcase_16 AC 1,078 ms
88,308 KB
testcase_17 AC 783 ms
58,188 KB
testcase_18 AC 745 ms
58,716 KB
testcase_19 AC 731 ms
58,496 KB
testcase_20 AC 712 ms
58,608 KB
testcase_21 AC 718 ms
58,568 KB
testcase_22 AC 54 ms
36,544 KB
testcase_23 AC 54 ms
36,772 KB
testcase_24 AC 53 ms
36,836 KB
testcase_25 AC 52 ms
36,784 KB
testcase_26 AC 53 ms
36,688 KB
testcase_27 AC 54 ms
36,812 KB
testcase_28 AC 53 ms
36,760 KB
testcase_29 AC 53 ms
36,296 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 < 22; ++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