結果

問題 No.1607 Kth Maximum Card
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-13 00:03:29
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 979 ms / 3,500 ms
コード長 4,069 bytes
コンパイル時間 6,845 ms
コンパイル使用メモリ 158,524 KB
実行使用メモリ 212,408 KB
最終ジャッジ日時 2024-02-13 00:03:56
合計ジャッジ時間 21,308 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
33,060 KB
testcase_01 AC 85 ms
33,060 KB
testcase_02 AC 81 ms
33,060 KB
testcase_03 AC 80 ms
33,060 KB
testcase_04 AC 81 ms
33,060 KB
testcase_05 AC 78 ms
33,060 KB
testcase_06 AC 81 ms
33,060 KB
testcase_07 AC 81 ms
33,060 KB
testcase_08 AC 979 ms
77,084 KB
testcase_09 AC 757 ms
74,292 KB
testcase_10 AC 904 ms
79,660 KB
testcase_11 AC 234 ms
58,200 KB
testcase_12 AC 629 ms
74,856 KB
testcase_13 AC 160 ms
48,164 KB
testcase_14 AC 180 ms
52,900 KB
testcase_15 AC 590 ms
67,756 KB
testcase_16 AC 162 ms
50,596 KB
testcase_17 AC 108 ms
38,564 KB
testcase_18 AC 405 ms
68,664 KB
testcase_19 AC 279 ms
63,692 KB
testcase_20 AC 320 ms
63,692 KB
testcase_21 AC 349 ms
64,708 KB
testcase_22 AC 797 ms
77,892 KB
testcase_23 AC 832 ms
77,888 KB
testcase_24 AC 296 ms
67,244 KB
testcase_25 AC 208 ms
53,372 KB
testcase_26 AC 229 ms
57,296 KB
testcase_27 AC 331 ms
67,276 KB
testcase_28 AC 248 ms
60,564 KB
testcase_29 AC 461 ms
64,816 KB
testcase_30 AC 901 ms
73,596 KB
testcase_31 AC 452 ms
64,892 KB
testcase_32 AC 307 ms
64,920 KB
testcase_33 AC 291 ms
64,920 KB
testcase_34 AC 289 ms
64,920 KB
testcase_35 AC 290 ms
212,408 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (94 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m, k) = (c[0], c[1], c[2]);
        var map = NArr(m);
        var tree = new List<(int to, int num)>[n];
        for (var i = 0; i < n; ++i) tree[i] = new List<(int to, int num)>();
        foreach (var edge in map)
        {
            tree[edge[0] - 1].Add((edge[1] - 1, edge[2]));
            tree[edge[1] - 1].Add((edge[0] - 1, edge[2]));
        }
        var ok = 200001;
        var ng = -1;
        while (ok - ng > 1)
        {
            var mid = (ok + ng) / 2;
            if (Check(tree, k, mid)) ok = mid;
            else ng = mid;
        }
        WriteLine(ok);
    }
    static bool Check(List<(int to, int num)>[] tree, int k, int mid)
    {
        var q = new Deque<(int pos, int len)>();
        q.Enqueue((0, 0));
        var len = Enumerable.Repeat(int.MaxValue / 2, tree.Length).ToArray();
        len[0] = 0;
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            if (cur.len != len[cur.pos]) continue;
            foreach (var next in tree[cur.pos])
            {
                var cost = next.num > mid ? 1 : 0;
                if (len[next.to] <= cur.len + cost) continue;
                len[next.to] = cur.len + cost;
                if (cost == 0) q.PushFront((next.to, len[next.to]));
                else q.Enqueue((next.to, len[next.to]));
            }
        }
        return len[^1] < k;
    }
    /// 前後から出し入れ可能なキュー
    class Deque<T>
    {
        int _s = 0;
        int _t = 0;
        T[] _arr;
        public Deque() : this(4)
        {
        }
        public Deque(int size)
        {
            _arr = new T[size];
        }
        public Deque(IEnumerable<T> init)
        {
            var list = init.ToArray();
            var size = 4;
            while (size <= list.Length) size <<= 1;
            _arr = new T[size];
            for (var i = 0; i < list.Length; ++i) _arr[i] = list[i];
            _t = _arr.Length;
        }
        public int Count
        {
            get { return (_t + _arr.Length - _s) % _arr.Length; }
        }
        public void Enqueue(T item)
        {
            if (Count + 1 == _arr.Length) Expand();
            if (_t == _arr.Length) _t = 0;
            _arr[_t++] = item;
        }
        public T Dequeue()
        {
            var ret = _arr[_s++];
            if (_s == _arr.Length) _s = 0;
            return ret;
        }
        public void Push(T item)
        {
            Enqueue(item);
        }
        public void PushFront(T item)
        {
            if (Count + 1 == _arr.Length) Expand();
            if (_s == 0) _s = _arr.Length;
            _arr[--_s] = item;
        }
        public T Pop()
        {
            var ret = _arr[--_t];
            if (_t == 0) _t = _arr.Length;
            return ret;
        }
        public T Peek()
        {
            if (Count == 0) throw new InvalidOperationException("deque is empty");
            return _arr[_s];
        }
        void Expand()
        {
            var count = Count;
            var na = new T[_arr.Length * 2];
            var cur = 0;
            if (_s <= _t)
            {
                for (var i = _s; i < _t; ++i) na[cur++] = _arr[i];
            }
            else
            {
                for (var i = _s; i < _arr.Length; ++i) na[cur++] = _arr[i];
                for (var i = 0; i < _t; ++i) na[cur++] = _arr[i];
            }
            _s = 0;
            _t = count;
            _arr = na;
        }
    }
}
0