結果

問題 No.2740 Old Maid
ユーザー 👑 kakel-sankakel-san
提出日時 2024-05-05 23:01:29
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,222 ms / 2,000 ms
コード長 9,590 bytes
コンパイル時間 12,208 ms
コンパイル使用メモリ 169,416 KB
実行使用メモリ 193,820 KB
最終ジャッジ日時 2024-05-05 23:02:20
合計ジャッジ時間 48,959 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 817 ms
91,608 KB
testcase_01 AC 815 ms
91,904 KB
testcase_02 AC 790 ms
91,904 KB
testcase_03 AC 824 ms
91,736 KB
testcase_04 AC 829 ms
91,776 KB
testcase_05 AC 795 ms
91,764 KB
testcase_06 AC 830 ms
91,604 KB
testcase_07 AC 827 ms
91,776 KB
testcase_08 AC 795 ms
91,856 KB
testcase_09 AC 814 ms
91,728 KB
testcase_10 AC 804 ms
91,732 KB
testcase_11 AC 882 ms
91,740 KB
testcase_12 AC 790 ms
75,480 KB
testcase_13 AC 946 ms
77,824 KB
testcase_14 AC 247 ms
48,384 KB
testcase_15 AC 332 ms
51,200 KB
testcase_16 AC 650 ms
67,436 KB
testcase_17 AC 225 ms
48,120 KB
testcase_18 AC 866 ms
76,672 KB
testcase_19 AC 398 ms
55,788 KB
testcase_20 AC 1,009 ms
84,224 KB
testcase_21 AC 263 ms
49,280 KB
testcase_22 AC 615 ms
66,688 KB
testcase_23 AC 188 ms
46,060 KB
testcase_24 AC 395 ms
55,168 KB
testcase_25 AC 1,114 ms
86,400 KB
testcase_26 AC 78 ms
31,872 KB
testcase_27 AC 167 ms
44,416 KB
testcase_28 AC 656 ms
67,328 KB
testcase_29 AC 608 ms
66,048 KB
testcase_30 AC 82 ms
32,512 KB
testcase_31 AC 1,222 ms
90,788 KB
testcase_32 AC 403 ms
57,600 KB
testcase_33 AC 1,169 ms
88,060 KB
testcase_34 AC 592 ms
65,516 KB
testcase_35 AC 343 ms
54,752 KB
testcase_36 AC 403 ms
55,624 KB
testcase_37 AC 470 ms
60,800 KB
testcase_38 AC 307 ms
53,632 KB
testcase_39 AC 220 ms
47,616 KB
testcase_40 AC 723 ms
71,936 KB
testcase_41 AC 1,029 ms
83,840 KB
testcase_42 AC 63 ms
31,096 KB
testcase_43 AC 65 ms
30,848 KB
testcase_44 AC 63 ms
30,976 KB
testcase_45 AC 63 ms
31,104 KB
testcase_46 AC 63 ms
31,100 KB
testcase_47 AC 63 ms
30,844 KB
testcase_48 AC 63 ms
30,848 KB
testcase_49 AC 64 ms
31,104 KB
testcase_50 AC 64 ms
31,104 KB
testcase_51 AC 63 ms
30,964 KB
testcase_52 AC 63 ms
30,976 KB
testcase_53 AC 62 ms
30,964 KB
testcase_54 AC 62 ms
31,096 KB
testcase_55 AC 63 ms
30,720 KB
testcase_56 AC 64 ms
30,720 KB
testcase_57 AC 63 ms
30,964 KB
testcase_58 AC 62 ms
31,100 KB
testcase_59 AC 62 ms
31,232 KB
testcase_60 AC 62 ms
30,964 KB
testcase_61 AC 63 ms
31,232 KB
testcase_62 AC 62 ms
31,360 KB
testcase_63 AC 63 ms
31,104 KB
testcase_64 AC 66 ms
193,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.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 n = NN;
        var p = NList;
        var plist = new Pair[n];
        var ntree = new AvlTree<int>();
        var tree = new AvlTree<Pair>();
        for (var i = 0; i < n; ++i)
        {
            ntree.Insert(i);
            plist[p[i] - 1] = new Pair(p[i] - 1, i);
            tree.Insert(plist[p[i] - 1]);
        }
        var q = new List<int>(n);
        while (ntree.GetCount() > 1)
        {
            var pos = ntree.ElementAt(0);
            var pidx = 0;
            tree.LowerBound(plist[pos], ref pidx);
            if (pidx + 1 < tree.GetCount())
            {
                q.Add(plist[pos].Id + 1);
                tree.Delete(plist[pos]);
                var next = tree.ElementAt(pidx);
                q.Add(next.Id + 1);
                tree.Delete(next);
                ntree.Delete(pos);
                ntree.Delete(next.Id);
            }
            else
            {
                pos = ntree.ElementAt(1);
                tree.LowerBound(plist[pos], ref pidx);
                q.Add(plist[pos].Id + 1);
                tree.Delete(plist[pos]);
                var next = tree.ElementAt(pidx);
                q.Add(next.Id + 1);
                tree.Delete(next);
                ntree.Delete(pos);
                ntree.Delete(next.Id);
            }
        }
        WriteLine(string.Join(" ", q));
    }
    class Pair : IComparable<Pair>
    {
        public int Id;
        public int Pos;
        public Pair(int id, int pos)
        {
            Id = id; Pos = pos;
        }
        public int CompareTo(Pair other)
        {
            return Pos.CompareTo(other.Pos);
        }
    }
    public class AvlTree<T> where T: IComparable<T>
    {
        private class Node<U> where U: IComparable<U>
        {
            public U Value;
            public Node<U> Lc = null;
            public Node<U> Rc = null;
            public int Height;
            public int Count;
            public int CCount;
            public Node(int height, U value)
            {
                Height = height;
                Count = 1;
                CCount = 1;
                Value = value;
            }
        }
        private Node<T> root = null;
        static int GetHeight(Node<T> t)
        {
            return t == null ? 0 : t.Height;
        }
        static int GetCount(Node<T> t)
        {
            return t == null ? 0 : t.Count;
        }
        static int GetBalance(Node<T> t)
        {
            return GetHeight(t.Lc) - GetHeight(t.Rc);
        }
        static void Recalc(Node<T> t)
        {
            if (t == null) return;
            t.Height = 1 + Math.Max(GetHeight(t.Lc), GetHeight(t.Rc));
            t.Count = t.CCount + GetCount(t.Lc) + GetCount(t.Rc);
        }
        static Node<T> RotateLeft(Node<T> t)
        {
            Node<T> u = t.Rc;
            Node<T> t2 = u.Lc;
            u.Lc = t;
            t.Rc = t2;
            Recalc(t);
            Recalc(u);
            return u;
        }
        static Node<T> RotateRight(Node<T> t)
        {
            Node<T> u = t.Lc;
            Node<T> t2 = u.Rc;
            u.Rc = t;
            t.Lc = t2;
            Recalc(t);
            Recalc(u);
            return u;
        }
        static Node<T> RotateLR(Node<T> t)
        {
            t.Lc = RotateLeft(t.Lc);
            return RotateRight(t);
        }
        static Node<T> RotateRL(Node<T> t)
        {
            t.Rc = RotateRight(t.Rc);
            return RotateLeft(t);
        }
        static Node<T> BalanceLeft(Node<T> t)
        {
            if (GetBalance(t) > 1)
            {
                if (GetBalance(t.Lc) < 0) t = RotateLR(t);
                else t = RotateRight(t);
            }
            Recalc(t);
            return t;
        }
        static Node<T> BalanceRight(Node<T> t)
        {
            if (GetBalance(t) < -1)
            {
                if (GetBalance(t.Rc) > 0) t = RotateRL(t);
                else t = RotateLeft(t);
            }
            Recalc(t);
            return t;
        }
        /// <summary>valueを挿入する</summary>
        public void Insert(T value)
        {
            root = Insert(root, value);
        }
        Node<T> Insert(Node<T> cur, T value)
        {
            if (cur == null)
            {
                return new Node<T>(1, value);
            }
            var d = value.CompareTo(cur.Value);
            if (d < 0)
            {
                cur.Lc = Insert(cur.Lc, value);
                return BalanceLeft(cur);
            }
            else if (d > 0)
            {
                cur.Rc = Insert(cur.Rc, value);
                return BalanceRight(cur);
            }
            else
            {
                ++cur.CCount;
                Recalc(cur);
                return cur;
            }
        }
        /// <summary>valueを削除する 存在しない場合はなにもしない</summary>
        public void Delete(T value)
        {
            root = Delete(root, value);
        }
        Node<T> Delete(Node<T> cur, T value)
        {
            if (cur == null) return null;
            var d = value.CompareTo(cur.Value);
            if (d < 0)
            {
                cur.Lc = Delete(cur.Lc, value);
                return BalanceRight(cur);
            }
            else if (d > 0)
            {
                cur.Rc = Delete(cur.Rc, value);
                return BalanceLeft(cur);
            }
            else
            {
                --cur.CCount;
                if (cur.CCount == 0)
                {
                    if (cur.Lc != null)
                    {
                        Node<T> del = null;
                        var max = DeleteMax(cur.Lc, ref del);
                        cur.Lc = max;
                        cur.Value = del.Value;
                        cur.CCount = del.CCount;
                        return BalanceRight(cur);
                    }
                    else
                    {
                        return cur.Rc;
                    }
                }
                else
                {
                    Recalc(cur);
                    return cur;
                }
            }
        }
        Node<T> DeleteMax(Node<T> t, ref Node<T> del)
        {
            if (t.Rc == null)
            {
                del = t;
                return t.Lc;
            }
            else
            {
                var max = DeleteMax(t.Rc, ref del);
                t.Rc = max;
                return BalanceLeft(t);
            }
        }
        /// <summary>小さいほうからpos番目の要素を取得する</summary>
        public T ElementAt(int pos)
        {
            if (pos < 0 || pos >= GetCount(root)) return default;
            var t = ElementAt(root, pos);
            return t.Value;
        }
        /// <summary>小さいほうからpos番目の要素を取得する</summary>
        public T ElementAt(int pos, T defaultValue)
        {
            if (pos < 0 || pos >= GetCount(root)) return defaultValue;
            return ElementAt(pos);
        }
        Node<T> ElementAt(Node<T> cur, int pos)
        {
            if (pos < GetCount(cur.Lc)) return ElementAt(cur.Lc, pos);
            if (pos < GetCount(cur.Lc) + cur.CCount) return cur;
            if (pos < cur.Count) return ElementAt(cur.Rc, pos - GetCount(cur.Lc) - cur.CCount);
            return null;
        }
        public void Debug()
        {
            if (root == null) return;
            Console.WriteLine($"root val:{root.Value}, CCount:{root.CCount}, Count:{root.Count}");
            Debug(root.Value, root.Lc);
            Debug(root.Value, root.Rc);
        }
        void Debug(T pval, Node<T> cur)
        {
            if (cur == null) return;
            Console.WriteLine($"{pval} -> val:{cur.Value}, CCount:{cur.CCount}, Count:{cur.Count}");
            Debug(cur.Value, cur.Lc);
            Debug(cur.Value, cur.Rc);
        }
        /// <summary>value以上でもっとも小さい値とその場所を返却する</summary>
        public T LowerBound(T value, ref int index)
        {
            var cur = root;
            T ans = default;
            var add = 0;
            index = GetCount(root);
            while (cur != null)
            {
                if (value.CompareTo(cur.Value) <= 0)
                {
                    ans = cur.Value;
                    index = add + GetCount(cur.Lc);
                    cur = cur.Lc;
                }
                else
                {
                    add += GetCount(cur.Lc) + cur.CCount;
                    cur = cur.Rc;
                }
            }
            return ans;
        }
        /// <summary>要素の個数を求める</summary>
        public int GetCount()
        {
            return GetCount(root);
        }
    }
}
0