結果

問題 No.2248 max(C)-min(C)
ユーザー 👑 kakel-sankakel-san
提出日時 2023-03-17 22:37:48
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 908 ms / 3,000 ms
コード長 3,813 bytes
コンパイル時間 5,745 ms
コンパイル使用メモリ 113,312 KB
実行使用メモリ 68,368 KB
最終ジャッジ日時 2023-10-18 15:34:39
合計ジャッジ時間 30,332 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
25,404 KB
testcase_01 AC 30 ms
25,396 KB
testcase_02 AC 29 ms
25,404 KB
testcase_03 AC 35 ms
25,752 KB
testcase_04 AC 37 ms
25,752 KB
testcase_05 AC 34 ms
25,744 KB
testcase_06 AC 38 ms
25,952 KB
testcase_07 AC 38 ms
26,016 KB
testcase_08 AC 38 ms
26,016 KB
testcase_09 AC 37 ms
26,016 KB
testcase_10 AC 35 ms
25,744 KB
testcase_11 AC 38 ms
26,016 KB
testcase_12 AC 35 ms
25,752 KB
testcase_13 AC 36 ms
25,924 KB
testcase_14 AC 37 ms
26,016 KB
testcase_15 AC 37 ms
26,016 KB
testcase_16 AC 36 ms
25,752 KB
testcase_17 AC 37 ms
25,980 KB
testcase_18 AC 716 ms
65,160 KB
testcase_19 AC 90 ms
29,788 KB
testcase_20 AC 855 ms
62,568 KB
testcase_21 AC 789 ms
59,972 KB
testcase_22 AC 493 ms
50,352 KB
testcase_23 AC 307 ms
48,664 KB
testcase_24 AC 128 ms
37,212 KB
testcase_25 AC 715 ms
64,572 KB
testcase_26 AC 654 ms
67,048 KB
testcase_27 AC 733 ms
64,304 KB
testcase_28 AC 78 ms
29,188 KB
testcase_29 AC 681 ms
63,720 KB
testcase_30 AC 240 ms
45,940 KB
testcase_31 AC 843 ms
62,580 KB
testcase_32 AC 142 ms
38,376 KB
testcase_33 AC 223 ms
44,032 KB
testcase_34 AC 858 ms
62,596 KB
testcase_35 AC 859 ms
62,584 KB
testcase_36 AC 908 ms
62,576 KB
testcase_37 AC 398 ms
49,380 KB
testcase_38 AC 859 ms
66,624 KB
testcase_39 AC 847 ms
66,628 KB
testcase_40 AC 840 ms
66,616 KB
testcase_41 AC 757 ms
67,904 KB
testcase_42 AC 839 ms
66,616 KB
testcase_43 AC 706 ms
68,304 KB
testcase_44 AC 813 ms
66,604 KB
testcase_45 AC 597 ms
51,276 KB
testcase_46 AC 290 ms
47,040 KB
testcase_47 AC 835 ms
66,628 KB
testcase_48 AC 428 ms
68,368 KB
testcase_49 AC 899 ms
62,664 KB
testcase_50 AC 860 ms
62,660 KB
testcase_51 AC 859 ms
62,660 KB
testcase_52 AC 870 ms
62,656 KB
testcase_53 AC 127 ms
37,120 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

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[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var a = NList;
        var b = NList;
        var s = new int[n];
        var m = new int[n];
        var l = new int[n];
        for (var i = 0; i < n; ++i)
        {
            s[i] = Math.Min(a[i], b[i]);
            m[i] = (a[i] + b[i]) / 2;
            l[i] = Math.Max(a[i], b[i]);
        }
        var q = new PriorityQueue<Pair>(n, false);
        var max = 0;
        for (var i = 0; i < n; ++i)
        {
            q.Enqueue(new Pair(i, 0, s[i]));
            max = Math.Max(max, s[i]);
        }
        var ans = l.Max() - l.Min();
        var lmin = int.MaxValue;
        while (q.Count > 0)
        {
            ans = Math.Min(ans, max - Math.Min(q.List[0].Val, lmin));
            var cur = q.Dequeue();
            if (cur.Kind == 0)
            {
                q.Enqueue(new Pair(cur.Pos, 1, m[cur.Pos]));
                max = Math.Max(max, m[cur.Pos]);
            }
            else if (cur.Kind == 1)
            {
                max = Math.Max(max, l[cur.Pos]);
                lmin = Math.Min(lmin, l[cur.Pos]);
            }
        }
        WriteLine(ans);
    }
    class Pair : IComparable<Pair>
    {
        public int Pos;
        public int Kind;
        public int Val;
        public Pair(int pos, int kind, int val)
        {
            Pos = pos; Kind = kind; Val = val;
        }
        public int CompareTo(Pair b)
        {
            return Val.CompareTo(b.Val);
        }
    }
    class PriorityQueue<T> where T : IComparable<T>
    {
        public T[] List;
        public int Count;
        bool IsTopMax;
        public PriorityQueue(int count, bool isTopMax)
        {
            IsTopMax = isTopMax;
            List = new T[Math.Max(128, count)];
        }
        public void Enqueue(T value)
        {
            if (Count == List.Length)
            {
                var newlist = new T[List.Length * 2];
                for (var i = 0; i < List.Length; ++i) newlist[i] = List[i];
                List = newlist;
            }
            var pos = Count;
            List[pos] = value;
            ++Count;
            while (pos > 0)
            {
                var parent = (pos - 1) / 2;
                if (Calc(List[parent], List[pos], true)) break;
                Swap(parent, pos);
                pos = parent;
            }
        }
        public T Dequeue()
        {
            --Count;
            Swap(0, Count);
            var pos = 0;
            while (true)
            {
                var child = pos * 2 + 1;
                if (child >= Count) break;
                if (child + 1 < Count && Calc(List[child + 1], List[child], false)) ++child;
                if (Calc(List[pos], List[child], true)) break;
                Swap(pos, child);
                pos = child;
            }
            return List[Count];
        }
        bool Calc(T a, T b, bool equalVal)
        {
            var ret = a.CompareTo(b);
            if (ret == 0 && equalVal) return true;
            return IsTopMax ? ret > 0 : ret < 0;
        }
        void Swap(int a, int b)
        {
            var tmp = List[a];
            List[a] = List[b];
            List[b] = tmp;
        }
    }
}
0