AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC356-E Max/Min


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            WillReturn.Add("3 1 4");
            //8
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("6");
            WillReturn.Add("2 7 1 8 2 8");
            //53
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("12");
            WillReturn.Add("3 31 314 3141 31415 314159 2 27 271 2718 27182 271828");
            //592622
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] AArr = GetSplitArr(InputList[1]);

        var CntDict = new Dictionary<long, long>();
        foreach (long EachA in AArr) {
            if (CntDict.ContainsKey(EachA) == false) {
                CntDict[EachA] = 0;
            }
            CntDict[EachA]++;
        }

        // 個数[値]なフェニック木
        var Ins_Fenwick_Tree = new Fenwick_Tree(AArr.Max());
        foreach (long EachA in AArr) {
            Ins_Fenwick_Tree[EachA]++;
        }

        long Answer = 0;
        foreach (var EachPair in CntDict) {
            long Choose2 = EachPair.Value * (EachPair.Value - 1) / 2;
            Answer += Choose2;
            long Bunbo = EachPair.Key;
            if (Bunbo == 1) {
                Answer += EachPair.Value * CntDict.Where(pX => pX.Key > 1).Sum(pX => pX.Key * pX.Value);
            }
            if (Bunbo >= 2) {
                long RangeSta = Bunbo + 1;
                long RangeEnd = Bunbo * 2 - 1;

                while (RangeSta <= Ins_Fenwick_Tree.GetUB()) {
                    RangeEnd = Math.Min(RangeEnd, Ins_Fenwick_Tree.GetUB());
                    long Syou = RangeSta / Bunbo;
                    Answer += EachPair.Value * Syou * Ins_Fenwick_Tree.GetSum(RangeSta, RangeEnd);

                    RangeSta = RangeEnd + 1;
                    RangeEnd = RangeSta + Bunbo - 1;
                }
            }
        }
        Console.WriteLine(Answer);
    }
}

// フェニック木
#region Fenwick_Tree
internal class Fenwick_Tree
{
    private long[] mBitArr;
    private long mExternalArrUB;

    // ノードのIndexの列挙を返す
    internal IEnumerable<long> GetNodeIndEnum()
    {
        for (long I = 0; I <= mExternalArrUB; I++) {
            yield return I;
        }
    }

    // 木のノードのUBを返す
    internal long GetUB()
    {
        return mExternalArrUB;
    }

    // コンストラクタ(外部配列のUBのみ指定)
    internal Fenwick_Tree(long pExternalArrUB)
    {
        mExternalArrUB = pExternalArrUB;

        // フェニック木の外部配列は0オリジンで、
        // フェニック木の内部配列は1オリジンなため、2を足す
        mBitArr = new long[pExternalArrUB + 2];
    }

    // コンストラクタ(初期化用の配列指定)
    internal Fenwick_Tree(long[] pArr)
        : this(pArr.GetUpperBound(0))
    {
        for (long I = 0; I <= pArr.GetUpperBound(0); I++) {
            this.Add(I, pArr[I]);
        }
    }

    // コンストラクタ(初期化用のList指定)
    internal Fenwick_Tree(List<long> pList)
        : this(pList.Count - 1)
    {
        for (int I = 0; I <= pList.Count - 1; I++) {
            this.Add(I, pList[I]);
        }
    }

    // Indのチェック
    private void IndCheck(long pInd)
    {
        if (pInd < 0) throw new Exception("pInd < 0");
        if (mExternalArrUB < pInd) throw new Exception("UB < pInd");
    }

    // Indの大小チェック
    private void IndRangeCheck(long pSta, long pEnd)
    {
        IndCheck(pSta);
        IndCheck(pEnd);
        if (pSta > pEnd) throw new Exception("pSta > pEnd");
    }

    // インデクサ
    internal long this[long pInd]
    {
        get { return GetSum(pInd, pInd); }
        set { Add(pInd, value - GetSum(pInd, pInd)); }
    }

    // [pSta,pEnd] のSumを返す
    internal long GetSum(long pSta, long pEnd)
    {
        IndRangeCheck(pSta, pEnd);

        long Result = GetSum(pEnd);
        if (pSta > 0) {
            Result -= GetSum(pSta - 1);
        }
        return Result;
    }

    // [0,pEnd] のSumを返す
    internal long GetSum(long pEnd)
    {
        IndCheck(pEnd);

        pEnd++; // 1オリジンに変更

        long Sum = 0;
        while (pEnd >= 1) {
            Sum += mBitArr[pEnd];
            pEnd -= pEnd & -pEnd;
        }
        return Sum;
    }

    // [I] に Xを加算
    internal void Add(long pI, long pX)
    {
        IndCheck(pI);

        pI++; // 1オリジンに変更

        while (pI <= mBitArr.GetUpperBound(0)) {
            mBitArr[pI] += pX;
            pI += pI & -pI;
        }
    }
}
#endregion


解説

全ペアなので、ソートして考えても、解は変わらないので、
1 1 1 2 2 2 3 3 3 4 5 6 10 100 1000 2000
という例で考えます。

まず、重複値から2つ選ぶペア数は、
度数分布表を求めてから、Chooseの公式で求めることができます。

次に、分母ごとの、商の総和は、
個数[値]なフェニック木の区間和で求めることができます。
これは
商が1になる区間
商が2になる区間
商が3になる区間
商が4になる区間
商が5になる区間
を全探索しても、調和級数の計算量なので間に合います。

計算量は、調和級数とフェニック木の組合せなので
n * log(n) * lon(n)
となります。