結果

問題 No.1170 Never Want to Walk
ユーザー Lily89164763Lily89164763
提出日時 2020-11-21 00:57:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 9,569 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 111,080 KB
実行使用メモリ 48,184 KB
最終ジャッジ日時 2023-09-30 20:24:19
合計ジャッジ時間 9,976 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
23,672 KB
testcase_01 AC 61 ms
21,544 KB
testcase_02 AC 62 ms
21,704 KB
testcase_03 AC 62 ms
23,672 KB
testcase_04 AC 62 ms
23,592 KB
testcase_05 AC 61 ms
21,592 KB
testcase_06 AC 62 ms
21,684 KB
testcase_07 AC 62 ms
21,696 KB
testcase_08 AC 62 ms
21,764 KB
testcase_09 AC 62 ms
23,840 KB
testcase_10 AC 61 ms
21,676 KB
testcase_11 AC 61 ms
21,800 KB
testcase_12 AC 70 ms
23,908 KB
testcase_13 AC 70 ms
21,840 KB
testcase_14 AC 69 ms
19,724 KB
testcase_15 AC 69 ms
23,984 KB
testcase_16 AC 69 ms
21,752 KB
testcase_17 AC 70 ms
21,852 KB
testcase_18 AC 69 ms
23,816 KB
testcase_19 AC 69 ms
21,768 KB
testcase_20 AC 69 ms
21,732 KB
testcase_21 AC 68 ms
21,816 KB
testcase_22 AC 70 ms
21,836 KB
testcase_23 AC 72 ms
21,832 KB
testcase_24 AC 72 ms
21,856 KB
testcase_25 AC 72 ms
21,792 KB
testcase_26 AC 71 ms
21,848 KB
testcase_27 AC 282 ms
47,644 KB
testcase_28 AC 278 ms
46,112 KB
testcase_29 AC 286 ms
45,904 KB
testcase_30 AC 285 ms
47,968 KB
testcase_31 AC 277 ms
45,424 KB
testcase_32 AC 269 ms
47,668 KB
testcase_33 AC 269 ms
47,372 KB
testcase_34 AC 272 ms
44,016 KB
testcase_35 AC 271 ms
47,984 KB
testcase_36 AC 266 ms
45,548 KB
testcase_37 AC 268 ms
45,416 KB
testcase_38 AC 276 ms
48,184 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 System.Linq;
using CompLib.Util;
using System.Threading;
using CompLib.Algorithm;
using CompLib.DataStructure;

public class Program
{
    private int N, A, B;
    private int[] X;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.NextInt();
        B = sc.NextInt();
        X = sc.IntArray();


        var uf = new UnionFind(N);
        int[] imos = new int[N + 1];
        for (int i = 0; i < N; i++)
        {
            if (imos[i] > 0 && i + 1 < N)
            {
                uf.Connect(i, i + 1);
            }

            // A以上
            var l = Algorithm.LowerBound(X, X[i] + A);

            // B超過
            var r = Algorithm.UpperBound(X, X[i] + B);

            // Console.WriteLine($"{i} {l} {r}");
            if (r > l)
            {
                uf.Connect(i, l);
                imos[l]++;
                imos[r - 1]--;
            }

            imos[i + 1] += imos[i];
        }

        int[] ans = new int[N];
        for (int i = 0; i < N; i++)
        {
            ans[i] = uf.GetSize(i);
        }

        Console.WriteLine(string.Join("\n", ans));
    }

    public static void Main(string[] args) => new Program().Solve();
    // public static void Main(string[] args) => new Thread(new Program().Solve, 1 << 27).Start();
}

namespace CompLib.Algorithm
{
    using System;
    using System.Collections.Generic;

    static class Algorithm
    {
        /// <summary>
        /// array[i] > targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <param name="comparison"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int UpperBound<T>(T[] array, T target, Comparison<T> comparison)
        {
            int ok = array.Length;
            int ng = -1;
            while (ok - ng > 1)
            {
                int med = (ok + ng) / 2;
                if (comparison(array[med], target) > 0)
                {
                    ok = med;
                }
                else
                {
                    ng = med;
                }
            }

            return ok;
        }

        /// <summary>
        /// array[i] > targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <param name="comparer"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int UpperBound<T>(T[] array, T target, Comparer<T> comparer) =>
            UpperBound(array, target, comparer.Compare);

        /// <summary>
        /// array[i] > targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int UpperBound<T>(T[] array, T target) => UpperBound(array, target, Comparer<T>.Default);

        /// <summary>
        /// array[i] >= targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <param name="comparison"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int LowerBound<T>(T[] array, T target, Comparison<T> comparison)
        {
            int ng = -1;
            int ok = array.Length;
            while (ok - ng > 1)
            {
                int med = (ok + ng) / 2;
                if (comparison(array[med], target) >= 0)
                {
                    ok = med;
                }
                else
                {
                    ng = med;
                }
            }

            return ok;
        }

        /// <summary>
        /// array[i] >= targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <param name="comparer"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int LowerBound<T>(T[] array, T target, Comparer<T> comparer) =>
            LowerBound(array, target, comparer.Compare);

        /// <summary>
        /// array[i] >= targetを満たす最小のiを求める
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int LowerBound<T>(T[] array, T target) => LowerBound(array, target, Comparer<T>.Default);
    }
}

namespace CompLib.DataStructure
{
    using System.Collections.Generic;
    using System.Linq;

    class UnionFind
    {
        private readonly int _n;
        private readonly int[] _parent, _size;

        /// <summary>
        /// n頂点の無向グラフに 1.辺を追加, 2.2頂点が同じ連結成分に属するか判定 ができるデータ構造
        /// </summary>
        /// <param name="n">頂点の個数</param>
        public UnionFind(int n)
        {
            _n = n;
            _parent = new int[_n];
            _size = new int[_n];
            for (int i = 0; i < _n; i++)
            {
                _parent[i] = i;
                _size[i] = 1;
            }
        }

        /// <summary>
        /// iがいる連結成分の代表値
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public int Find(int i) => _parent[i] == i ? i : Find(_parent[i]);

        /// <summary>
        /// x,yが同じ連結成分にいるか?
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Same(int x, int y) => Find(x) == Find(y);

        /// <summary>
        /// (x, y)に辺を追加する
        /// </summary>
        /// <remarks>
        /// ACLでは連結された代表値を返しますが、ここでは連結できたか?を返します
        /// </remarks>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>x,yが違う連結成分だったならtrueを返す</returns>
        public bool Connect(int x, int y)
        {
            x = Find(x);
            y = Find(y);
            if (x == y) return false;

            // データ構造をマージする一般的なテク
            if (_size[x] > _size[y])
            {
                _parent[y] = x;
                _size[x] += _size[y];
            }
            else
            {
                _parent[x] = y;
                _size[y] += _size[x];
            }

            return true;
        }

        /// <summary>
        /// iが含まれる成分のサイズ
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public int GetSize(int i) => _size[Find(i)];

        /// <summary>
        /// 連結成分のリスト
        /// </summary>
        /// <returns></returns>
        public List<int>[] Groups()
        {
            var leaderBuf = new int[_n];
            var groupSize = new int[_n];
            for (int i = 0; i < _n; i++)
            {
                leaderBuf[i] = Find(i);
                groupSize[leaderBuf[i]]++;
            }

            var result = new List<int>[_n];
            for (int i = 0; i < _n; i++)
            {
                result[i] = new List<int>(groupSize[i]);
            }

            for (int i = 0; i < _n; i++)
            {
                result[leaderBuf[i]].Add(i);
            }

            return result.Where(ls => ls.Count > 0).ToArray();
        }
    }
}


namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}
0