結果

問題 No.576 E869120 and Rings
ユーザー 37zigen37zigen
提出日時 2020-03-19 03:11:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,358 ms / 1,500 ms
コード長 3,764 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 74,872 KB
実行使用メモリ 90,392 KB
最終ジャッジ日時 2023-08-20 20:35:56
合計ジャッジ時間 26,387 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,157 ms
72,152 KB
testcase_01 AC 1,088 ms
72,280 KB
testcase_02 AC 1,258 ms
71,928 KB
testcase_03 AC 1,358 ms
72,084 KB
testcase_04 AC 1,168 ms
72,316 KB
testcase_05 AC 1,115 ms
71,984 KB
testcase_06 AC 1,172 ms
71,916 KB
testcase_07 AC 1,304 ms
72,208 KB
testcase_08 AC 835 ms
72,100 KB
testcase_09 AC 882 ms
71,944 KB
testcase_10 AC 838 ms
71,888 KB
testcase_11 AC 740 ms
72,068 KB
testcase_12 AC 805 ms
71,988 KB
testcase_13 AC 779 ms
71,832 KB
testcase_14 AC 863 ms
71,772 KB
testcase_15 AC 309 ms
67,720 KB
testcase_16 AC 1,124 ms
90,392 KB
testcase_17 AC 793 ms
72,100 KB
testcase_18 AC 753 ms
71,944 KB
testcase_19 AC 729 ms
72,300 KB
testcase_20 AC 736 ms
71,708 KB
testcase_21 AC 751 ms
72,112 KB
testcase_22 AC 47 ms
49,392 KB
testcase_23 AC 47 ms
49,192 KB
testcase_24 AC 49 ms
49,276 KB
testcase_25 AC 46 ms
48,896 KB
testcase_26 AC 48 ms
49,068 KB
testcase_27 AC 47 ms
49,296 KB
testcase_28 AC 47 ms
49,240 KB
testcase_29 AC 46 ms
49,104 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