結果

問題 No.1739 Princess vs. Dragoness (& AoE)
ユーザー O2MTO2MT
提出日時 2021-11-24 19:13:42
言語 C#
(.NET 8.0.203)
結果
TLE  
実行時間 -
コード長 5,266 bytes
コンパイル時間 11,548 ms
コンパイル使用メモリ 146,136 KB
実行使用メモリ 165,868 KB
最終ジャッジ日時 2023-09-09 12:00:29
合計ジャッジ時間 67,864 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
28,960 KB
testcase_01 AC 56 ms
29,188 KB
testcase_02 AC 56 ms
29,108 KB
testcase_03 AC 158 ms
54,296 KB
testcase_04 AC 1,928 ms
69,092 KB
testcase_05 TLE -
testcase_06 AC 536 ms
70,496 KB
testcase_07 AC 296 ms
54,580 KB
testcase_08 AC 1,578 ms
63,548 KB
testcase_09 AC 1,820 ms
68,972 KB
testcase_10 AC 714 ms
41,508 KB
testcase_11 AC 426 ms
38,320 KB
testcase_12 AC 60 ms
33,740 KB
testcase_13 AC 996 ms
65,264 KB
testcase_14 AC 1,993 ms
65,756 KB
testcase_15 AC 2,301 ms
66,712 KB
testcase_16 AC 1,353 ms
62,680 KB
testcase_17 AC 777 ms
55,164 KB
testcase_18 AC 1,221 ms
52,060 KB
testcase_19 AC 260 ms
49,056 KB
testcase_20 AC 875 ms
59,744 KB
testcase_21 AC 649 ms
50,104 KB
testcase_22 AC 2,158 ms
71,448 KB
testcase_23 AC 2,484 ms
72,288 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,857 ms
71,472 KB
testcase_27 TLE -
testcase_28 AC 1,921 ms
70,292 KB
testcase_29 TLE -
testcase_30 AC 2,887 ms
68,596 KB
testcase_31 AC 2,078 ms
69,380 KB
testcase_32 TLE -
testcase_33 AC 56 ms
29,480 KB
testcase_34 AC 59 ms
30,132 KB
testcase_35 AC 57 ms
29,332 KB
testcase_36 AC 56 ms
29,844 KB
testcase_37 AC 56 ms
30,608 KB
testcase_38 AC 58 ms
32,220 KB
testcase_39 AC 60 ms
32,172 KB
testcase_40 AC 58 ms
32,064 KB
testcase_41 AC 58 ms
30,208 KB
testcase_42 AC 59 ms
165,868 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 127 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;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            var (N, A, B, X, Y) = (arr[0], arr[1], arr[2], arr[3], arr[4]);
            var H = Console.ReadLine().Split(' ').Select(long.Parse).ToList();
            

            // 汎用的な二分探索のテンプレ
            long binary_search(List<long> H)
            {
                long ng = -1;
                long ok = (long)Math.Pow(10, 18);

                /* ok と ng のどちらが大きいかわからないことを考慮 */
                while (Math.Abs(ok - ng) > 1)
                {
                    long mid = (ok + ng) / 2;

                    if (isClearedAllMonsters(H, mid)) ok = mid;
                    else ng = mid;
                }
                return ok;
            }


            bool isClearedAllMonsters(List<long> H, long k)
            {
                var PQ = new PriorityQueue<long>((int)N);
                foreach (var h in H)
                {
                    if (h - k > 0) PQ.Push(h - k);
                }

                for (int i = 0; i < A && PQ.Count > 0; i++)
                {
                    var num = PQ.Pop();
                    if (num - X > 0)
                    {
                        PQ.Push(num - X);
                    }
                }
                for (int i = 0; i < B; i++)
                {
                    var YY = Y;
                    var list = new List<long>();
                    while (YY > 0 && PQ.Count > 0)
                    {
                        var num = PQ.Pop();
                        if (num > YY)
                        {
                            list.Add(num - YY);
                        }
                        YY -= num;
                    }
                    list.ForEach(PQ.Push);
                }
                if (PQ.Count > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

            var ans = binary_search(H);
            Console.WriteLine(ans);
        }
    }

    class PriorityQueue<T> where T : IComparable<T>
    {
        private readonly T[] _array;
        private readonly IComparer _comparer;
        public int Count { get; private set; } = 0;
        public T Root => _array[0];

        public PriorityQueue(int capacity, IComparer comparer = null)
        {
            _array = new T[capacity];
            _comparer = comparer;
        }


        /// <summary>
        /// 要素を挿入する
        /// </summary>
        ///
        public void Push(T item)
        {
            _array[this.Count] = item;
            Count += 1;

            var n = Count - 1;                  // 末尾(追加した)のノードの番号
            while (n != 0)
            {
                var parent = (n - 1) / 2;       // 親ノードの番号

                if (Compare(_array[n], _array[parent]) > 0)
                {
                    Swap(n, parent);
                    n = parent;
                }
                else
                {
                    break;
                }
            }
        }

        /// <summary>
        /// 優先度の一番高いものを取り出す
        /// </summary>
        public T Pop()
        {
            Swap(0, this.Count - 1);            // 先頭要素を末尾にする
            Count -= 1;

            var parent = 0;                     // 親ノードの番号
            while (true)
            {
                var child = 2 * parent + 1;     // 子ノードの番号
                if (child > Count - 1) break;

                // 値の大きい方の子を選ぶ
                if (child < Count - 1 && Compare(_array[child], _array[child + 1]) < 0) child += 1;

                // 子の方が親より大きければ入れ替える
                if (Compare(_array[parent], _array[child]) < 0)
                {
                    Swap(parent, child);
                    parent = child;
                }
                else
                {
                    break;
                }
            }

            return _array[Count];
        }

        /// <summary>
        /// 大きいものから列挙していく
        /// withPop = falseのときは順番関係なく取り出しもしないが素早く全要素を取得できる 
        /// </summary>
        public IEnumerable<T> GetAllElements(bool withPop = true)
        {
            int count = Count;
            for (int i = 0; i < count; i++)
            {
                if (withPop) yield return Pop();
                else yield return _array[count - i - 1];
            }
        }

        private int Compare(T a, T b)
        {
            if (_comparer == null) return a.CompareTo(b);
            return _comparer.Compare(a, b);
        }

        private void Swap(int a, int b)
        {
            var tmp = _array[a];
            _array[a] = _array[b];
            _array[b] = tmp;
        }
    }
}
0