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

ABC190-F Shift and Inversions


問題へのリンク


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("4");
            WillReturn.Add("0 1 2 3");
            //0
            //3
            //4
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("0 3 1 5 4 2 9 6 8 7");
            //9
            //18
            //21
            //28
            //27
            //28
            //33
            //24
            //21
            //14
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long N = long.Parse(InputList[0]);
        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        var AnsweList = new List<long>();
        AnsweList.Add(DeriveInvCnt(AArr));

        for (long I = 0; I <= AArr.GetUpperBound(0) - 1; I++) {
            long CurrInvCnt = AnsweList.Last();
            long CurrVal = AArr[I];
            CurrInvCnt -= CurrVal;
            CurrInvCnt += N - CurrVal - 1;
            AnsweList.Add(CurrInvCnt);
        }

        var sb = new System.Text.StringBuilder();
        AnsweList.ForEach(pX => sb.AppendLine(pX.ToString()));
        Console.Write(sb.ToString());
    }

    ////////////////////////////////////////////////////////////////
    // 列挙を引数として、列挙内での転倒数を返す
    ////////////////////////////////////////////////////////////////
    static long DeriveInvCnt(IEnumerable<long> pEnum)
    {
        long[] pArr = pEnum.ToArray();

        Dictionary<long, long> ZaatuDict = DeriveZaatuDict(pArr);

        long TentouCnt = 0;
        var Ins_Fenwick_Tree = new Fenwick_Tree(ZaatuDict.Count);
        long UB = ZaatuDict.Count - 1;

        foreach (long EachVal in pArr) {
            long ZaatuVal = ZaatuDict[EachVal];
            TentouCnt += Ins_Fenwick_Tree.GetSum(ZaatuVal + 1, UB, true);
            Ins_Fenwick_Tree.Add(ZaatuVal, 1, true);
        }
        return TentouCnt;
    }

    //////////////////////////////////////////////////////////////////////////
    // 列挙を引数として、座標圧縮し、座圧後の値[座圧前の値]なDictを返す
    //////////////////////////////////////////////////////////////////////////
    static Dictionary<long, long> DeriveZaatuDict(IEnumerable<long> pEnum)
    {
        var ZaatuDict = new Dictionary<long, long>();
        var ValSet = new HashSet<long>(pEnum);
        long No = 0;
        foreach (long EachVal in ValSet.OrderBy(pX => pX)) {
            ZaatuDict[EachVal] = No;
            No++;
        }
        return ZaatuDict;
    }
}

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

    // コンストラクタ
    internal Fenwick_Tree(long pItemCnt)
    {
        mN = pItemCnt;
        mBitArr = new long[pItemCnt + 1];
    }

    // [pSta,pEnd] のSumを返す
    internal long GetSum(long pSta, long pEnd, bool pIsZeroOrigin)
    {
        return GetSum(pEnd, pIsZeroOrigin) - GetSum(pSta - 1, pIsZeroOrigin);
    }

    // [0,pEnd] のSumを返す
    internal long GetSum(long pEnd, bool pIsZeroOrigin)
    {
        if (pIsZeroOrigin) {
            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, bool pIsZeroOrigin)
    {
        if (pIsZeroOrigin) {
            pI++; // 1オリジンに変更
        }

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


解説

実験すると

最初の数列の転倒数を求め、
以降のシフトによる転倒数の増減は、引き算と足し算で求めることができると分かります。