結果
問題 | No.576 E869120 and Rings |
ユーザー |
|
提出日時 | 2017-10-14 23:21:39 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
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;elsesum[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++];elsereturn -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));}}