結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-08 00:04:42
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 3,003 bytes
コンパイル時間 12,962 ms
コンパイル使用メモリ 158,908 KB
実行使用メモリ 236,720 KB
最終ジャッジ日時 2024-02-08 00:05:20
合計ジャッジ時間 35,894 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
33,316 KB
testcase_01 AC 80 ms
33,316 KB
testcase_02 AC 84 ms
33,316 KB
testcase_03 AC 1,489 ms
116,268 KB
testcase_04 AC 1,451 ms
118,408 KB
testcase_05 AC 91 ms
33,316 KB
testcase_06 AC 575 ms
86,852 KB
testcase_07 AC 1,129 ms
143,264 KB
testcase_08 AC 1,442 ms
141,924 KB
testcase_09 WA -
testcase_10 AC 954 ms
126,992 KB
testcase_11 AC 940 ms
129,944 KB
testcase_12 AC 949 ms
130,088 KB
testcase_13 AC 739 ms
89,668 KB
testcase_14 AC 572 ms
87,816 KB
testcase_15 AC 1,076 ms
136,776 KB
testcase_16 AC 629 ms
88,144 KB
testcase_17 AC 82 ms
33,316 KB
testcase_18 AC 1,117 ms
157,072 KB
testcase_19 AC 1,062 ms
136,776 KB
testcase_20 AC 949 ms
125,620 KB
testcase_21 AC 615 ms
97,224 KB
testcase_22 AC 650 ms
236,720 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 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 n = NN;
        var x = NList;
        var a = NList;
        var set = new HashSet<int>(x);
        for (var i = 0; i < n; ++i)
        {
            set.Add(x[i] - a[i]);
            set.Add(x[i] + a[i]);
        }
        var list = new List<int>(set);
        list.Sort();
        var dic = new Dictionary<int, int>();
        for (var i = 0; i < list.Count; ++i) dic[list[i]] = i;
        var tree = new List<int>[list.Count];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        var refs = new int[list.Count];
        for (var i = 0; i < n; ++i)
        {
            tree[dic[x[i] - a[i]]].Add(dic[x[i]]);
            tree[dic[x[i] + a[i]]].Add(dic[x[i]]);
            refs[dic[x[i]]] += 2;
        }
        var q = new Queue<int>();
        for (var i = 0; i < list.Count; ++i) if (refs[i] == 0) q.Enqueue(i);
        var max = list.ToList();
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            for (var i = tree[cur].Count - 1; i >= 0; --i)
            {
                max[tree[cur][i]] = Math.Max(max[tree[cur][i]], max[cur]);
                --refs[tree[cur][i]];
                if (refs[tree[cur][i]] == 0) q.Enqueue(tree[cur][i]);
                tree[cur].RemoveAt(i);
            }
        }
        var uf = new UnionFindTree(list.Count);
        for (var i = 0; i < list.Count; ++i) foreach (var next in tree[i]) uf.Unite(i, next);
        for (var i = 0; i < list.Count; ++i) max[uf.GetRoot(i)] = Math.Max(max[uf.GetRoot(i)], max[i]);
        WriteLine(string.Join("\n" , x.Select(xi => max[uf.GetRoot(dic[xi])] - xi)));
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0