結果

問題 No.576 E869120 and Rings
ユーザー 明智重蔵明智重蔵
提出日時 2022-12-31 12:45:18
言語 C#
(.NET 8.0.203)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,757 bytes
コンパイル時間 10,058 ms
コンパイル使用メモリ 143,020 KB
実行使用メモリ 161,948 KB
最終ジャッジ日時 2023-08-17 13:58:41
合計ジャッジ時間 24,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 771 ms
46,712 KB
testcase_01 AC 730 ms
44,948 KB
testcase_02 AC 709 ms
44,964 KB
testcase_03 AC 770 ms
43,020 KB
testcase_04 AC 760 ms
44,936 KB
testcase_05 AC 776 ms
45,192 KB
testcase_06 AC 772 ms
45,072 KB
testcase_07 AC 713 ms
46,912 KB
testcase_08 AC 558 ms
46,308 KB
testcase_09 AC 593 ms
47,584 KB
testcase_10 AC 677 ms
47,224 KB
testcase_11 AC 600 ms
48,740 KB
testcase_12 AC 606 ms
43,300 KB
testcase_13 AC 469 ms
47,944 KB
testcase_14 AC 525 ms
44,908 KB
testcase_15 AC 59 ms
35,272 KB
testcase_16 AC 451 ms
64,564 KB
testcase_17 AC 458 ms
45,164 KB
testcase_18 AC 525 ms
45,296 KB
testcase_19 AC 526 ms
45,044 KB
testcase_20 AC 514 ms
47,056 KB
testcase_21 AC 526 ms
45,160 KB
testcase_22 AC 55 ms
29,040 KB
testcase_23 AC 54 ms
28,712 KB
testcase_24 AC 55 ms
29,072 KB
testcase_25 AC 53 ms
28,760 KB
testcase_26 AC 55 ms
29,172 KB
testcase_27 AC 56 ms
28,876 KB
testcase_28 AC 55 ms
28,880 KB
testcase_29 MLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 166 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("8 4");
            WillReturn.Add("11101110");
            //0.857142857142857
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("8 4");
            WillReturn.Add("11011001");
            //0.833333333333333
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 4");
            WillReturn.Add("1001001001");
            //0.6
        }
        else if (InputPattern == "Input4") {
            return System.IO.File.ReadAllLines(@"D:\CSharp\TestConsole\test_in\in01.txt").ToList();
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mN;
    static int mK;

    static double[] mAArr;
    static double[] mCompareArr;
    //static double[] mRunSumArr;
    static int UB2;

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mN = wkArr[0];
        mK = wkArr[1];

        string AStr = InputList[1];
        mAArr = new double[mN];
        bool HasZero = false;
        for (int I = 0; I <= AStr.Length - 1; I++) {
            if (AStr[I] == '0') {
                mAArr[I] = 0D;
                HasZero = true;
            }
            if (AStr[I] == '1') mAArr[I] = 1D;
        }
        mCompareArr = new double[mN * 2];
        //mRunSumArr = new double[mN * 2];
        UB2 = mCompareArr.GetUpperBound(0);

        // 全て1の場合
        if (HasZero == false) {
            Console.WriteLine(1);
            return;
        }

        // 答えで二分探索
        double L = 0D;
        double R = 1D;

        while (L + 0.000001 < R) {
            double Mid = (L + R) / 2;

            bool Result = CanAchieve(Mid);
            if (Result) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        Console.WriteLine(R);
    }

    // NeedAVGを達成できるかを判定
    static bool CanAchieve(double pNeedAVG)
    {
        // 取得区間はK以上N以下なので、2倍の長さにしておく
        for (int I = 0; I <= mAArr.GetUpperBound(0); I++) {
            mCompareArr[I + mN] = mCompareArr[I] = mAArr[I] - pNeedAVG;
        }

        // 累積和を設定する
        //mRunSumArr = (double[])mCompareArr.Clone();
        //for (int I = 1; I <= UB2; I++) {
        //    mRunSumArr[I] += mRunSumArr[I - 1];
        //}
        for (int I = 1; I <= UB2; I++) {
            mCompareArr[I] += mCompareArr[I - 1];
        }

        var InsDeque = new Deque<int>();

        // 終端を全探索
        bool FirstFlag = true;
        int PrevRangeEnd = -1;

        for (int I = mN - 1; I <= UB2; I++) {
            // 始点候補のSta
            int RangeSta = I - mN + 1;

            // 始点候補のEnd
            int RangeEnd = I - mK + 1;

            if (FirstFlag) {
                FirstFlag = false;
            }
            else {
                RangeSta = Math.Max(RangeSta, PrevRangeEnd + 1);
            }

            // 引退処理
            while (InsDeque.Count > 0) {
                int FrontInd = InsDeque[0];
                if (RangeSta <= FrontInd && FrontInd <= RangeEnd == false) {
                    InsDeque.PopFront();
                    continue;
                }
                break;
            }

            for (int J = RangeSta; J <= RangeEnd; J++) {
                // 押し出し処理
                while (InsDeque.Count > 0) {
                    int LastInd = InsDeque[InsDeque.Count - 1];

                    double RunSum1 = 0D;
                    if (LastInd >= 1) {
                        RunSum1 = mCompareArr[LastInd - 1];
                    }

                    double RunSum2 = 0D;
                    if (J >= 1) {
                        RunSum2 = mCompareArr[J - 1];
                    }

                    //if (mRunSumArr[LastInd] - mCompareArr[LastInd] >= mRunSumArr[J] - mCompareArr[J]) {
                    if (RunSum1 >= RunSum2) {
                        InsDeque.PopBack();
                        continue;
                    }
                    break;
                }
                // 追加処理
                InsDeque.PushBack(J);
            }
            PrevRangeEnd = RangeEnd;

            // 最小値
            //double MinVal = mRunSumArr[InsDeque[0]] - mCompareArr[InsDeque[0]];
            int MinInd = InsDeque[0];
            double MinVal = 0D;
            if (MinInd >= 1) {
                MinVal = mCompareArr[MinInd - 1];
            }

            if (mCompareArr[I] - MinVal >= 0) return true;
        }
        return false;
    }
}

#region Deque
internal class Deque<T>
{
    T[] buf;
    int offset, count, capacity;

    internal int Count { get { return count; } }

    internal Deque(int cap) { buf = new T[capacity = cap]; }

    internal Deque() { buf = new T[capacity = 16]; }

    internal T this[int index]
    {
        get { return buf[GetIndex(index)]; }
        set { buf[GetIndex(index)] = value; }
    }

    private int GetIndex(int index)
    {
        if (index >= capacity)
            throw new IndexOutOfRangeException("out of range");
        int ret = index + offset;
        if (ret >= capacity)
            ret -= capacity;
        return ret;
    }

    internal void PushFront(T item)
    {
        if (count == capacity) Extend();
        if (--offset < 0) offset += buf.Length;
        buf[offset] = item;
        count++;
    }

    internal T PopFront()
    {
        if (count == 0)
            throw new InvalidOperationException("collection is empty");
        count--;
        T ret = buf[offset++];
        if (offset >= capacity) offset -= capacity;
        return ret;
    }

    internal void PushBack(T item)
    {
        if (count == capacity) Extend();
        int id = count + offset;
        count++;
        if (id >= capacity) id -= capacity;
        buf[id] = item;
    }

    internal T PopBack()
    {
        if (count == 0)
            throw new InvalidOperationException("collection is empty");
        return buf[GetIndex(--count)];
    }

    internal void Insert(int index, T item)
    {
        if (index > count) throw new IndexOutOfRangeException();
        this.PushFront(item);
        for (int i = 0; i < index; i++)
            this[i] = this[i + 1];
        this[index] = item;
    }

    internal T RemoveAt(int index)
    {
        if (index < 0 || index >= count) throw new IndexOutOfRangeException();
        T ret = this[index];
        for (int i = index; i > 0; i--)
            this[i] = this[i - 1];
        this.PopFront();
        return ret;
    }

    private void Extend()
    {
        T[] newBuffer = new T[capacity << 1];
        if (offset > capacity - count) {
            int len = buf.Length - offset;
            Array.Copy(buf, offset, newBuffer, 0, len);
            Array.Copy(buf, 0, newBuffer, len, count - len);
        }
        else Array.Copy(buf, offset, newBuffer, 0, count);
        buf = newBuffer;
        offset = 0;
        capacity <<= 1;
    }

    internal T[] Items//デバッグ時に中身を調べるためのプロパティ
    {
        get
        {
            T[] a = new T[count];
            for (int i = 0; i < count; i++)
                a[i] = this[i];
            return a;
        }
    }
}
#endregion
0