結果

問題 No.1950 片道きゃっちぼーる
ユーザー kakel-sankakel-san
提出日時 2024-02-08 00:15:11
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 941 ms / 3,000 ms
コード長 5,518 bytes
コンパイル時間 7,309 ms
コンパイル使用メモリ 165,412 KB
実行使用メモリ 251,540 KB
最終ジャッジ日時 2024-09-28 12:40:30
合計ジャッジ時間 19,438 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
31,616 KB
testcase_01 AC 60 ms
31,488 KB
testcase_02 AC 59 ms
31,360 KB
testcase_03 AC 529 ms
146,008 KB
testcase_04 AC 571 ms
185,700 KB
testcase_05 AC 62 ms
31,488 KB
testcase_06 AC 273 ms
124,288 KB
testcase_07 AC 578 ms
159,368 KB
testcase_08 AC 941 ms
169,252 KB
testcase_09 AC 408 ms
120,872 KB
testcase_10 AC 502 ms
131,340 KB
testcase_11 AC 486 ms
135,968 KB
testcase_12 AC 495 ms
135,584 KB
testcase_13 AC 390 ms
112,044 KB
testcase_14 AC 286 ms
97,152 KB
testcase_15 AC 609 ms
140,092 KB
testcase_16 AC 274 ms
117,888 KB
testcase_17 AC 61 ms
31,468 KB
testcase_18 AC 635 ms
167,040 KB
testcase_19 AC 606 ms
135,444 KB
testcase_20 AC 484 ms
131,100 KB
testcase_21 AC 312 ms
97,768 KB
testcase_22 AC 304 ms
251,540 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (86 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 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 g = new SCCGraph(list.Count);
        for (var i = 0; i < n; ++i)
        {
            g.AddEdge(dic[x[i] - a[i]], dic[x[i]]);
            g.AddEdge(dic[x[i] + a[i]], dic[x[i]]);
        }
        var scc = g.SCC();
        var gid = new int[list.Count];
        for (var i = 0; i < scc.Length; ++i) foreach (var j in scc[i]) gid[j] = i;
        var tree = new List<int>[scc.Length];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        var refs = new int[scc.Length];
        var max = new int[refs.Length];
        for (var i = 0; i < n; ++i)
        {
            var f = dic[x[i] - a[i]];
            var t = dic[x[i]];
            max[gid[t]] = Math.Max(max[gid[t]], x[i]);
            max[gid[f]] = Math.Max(max[gid[f]], x[i] - a[i]);
            if (gid[f] != gid[t])
            {
                tree[gid[f]].Add(gid[t]);
                ++refs[gid[t]];
            }
            f = dic[x[i] + a[i]];
            max[gid[f]] = Math.Max(max[gid[f]], x[i] + a[i]);
            if (gid[f] != gid[t])
            {
                tree[gid[f]].Add(gid[t]);
                ++refs[gid[t]];
            }
        }
        var q = new Queue<int>();
        for (var i = 0; i < refs.Length; ++i) if (refs[i] == 0) q.Enqueue(i);
        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);
            }
        }
        WriteLine(string.Join("\n" , x.Select(xi => max[gid[dic[xi]]] - xi)));
    }
    class SCCGraph
    {
        private int _n;
        private List<(int from, int to)> edges;
        public SCCGraph(int n)
        {
            _n = n;
            edges = new List<(int, int)>();
        }
        public int NumVertices() { return _n; }
        public void AddEdge(int from, int to) { edges.Add((from, to )); }
        public (int, int[]) SCCIds()
        {
            var g = new CSR<int>(_n, edges);
            var nowOrd = 0;
            var groupNum = 0;
            var visited = new List<int>(_n);
            var low = new int[_n];
            var ord = Enumerable.Repeat(-1, _n).ToArray();
            var ids = new int[_n];
            void DFS(int v)
            {
                low[v] = ord[v] = nowOrd++;
                visited.Add(v);
                for (var i = g.Start[v]; i < g.Start[v + 1]; ++i)
                {
                    var to = g.EList[i];
                    if (ord[to] == -1)
                    {
                        DFS(to);
                        low[v] = Math.Min(low[v], low[to]);
                    }
                    else low[v] = Math.Min(low[v], ord[to]);
                }
                if (low[v] == ord[v])
                {
                    while (true)
                    {
                        var u = visited.Last();
                        visited.RemoveAt(visited.Count - 1);
                        ord[u] = _n;
                        ids[u] = groupNum;
                        if (u == v) break;
                    }
                    ++groupNum;
                }
            }
            for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i);
            for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i];
            return (groupNum, ids);
        }
        public List<int>[] SCC()
        {
            var (groupNum, list) = SCCIds();
            var counts = new int[groupNum];
            foreach (var x in list) ++counts[x];
            var groups = new List<int>[groupNum];
            for (var i = 0; i < groups.Length; ++i) groups[i] = new List<int>(counts[i]);
            for (var i = 0; i < _n; ++i) groups[list[i]].Add(i);
            return groups;
        }
    }
    // 有向辺のリスト
    class CSR<E>
    {
        public int[] Start { get; private set; }
        public E[] EList { get; private set; }
        public CSR(int n, List<(int, E)> edges)
        {
            Start = new int[n + 1];
            EList = new E[edges.Count];
            foreach (var e in edges) ++Start[e.Item1 + 1];
            for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1];
            var counter = (int[])Start.Clone();
            foreach (var e in edges)
            {
                EList[counter[e.Item1]++] = e.Item2;
            }
        }
    }
}
0