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

ABC261-F Sorting Color Balls


問題へのリンク


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("5");
            WillReturn.Add("1 5 2 2 1");
            WillReturn.Add("3 2 1 2 1");
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3");
            WillReturn.Add("1 1 1");
            WillReturn.Add("3 2 1");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3");
            WillReturn.Add("3 1 2");
            WillReturn.Add("1 1 2");
            //0
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        long Answer = DeriveInvCnt(XArr);
        var ListDict = new Dictionary<long, List<long>>();
        for (long I = 0; I <= CArr.GetUpperBound(0); I++) {
            if (ListDict.ContainsKey(CArr[I]) == false) {
                ListDict[CArr[I]] = new List<long>();
            }
            ListDict[CArr[I]].Add(XArr[I]);
        }
        //Console.WriteLine("全体の転倒数={0}", Answer);

        foreach (var EachPair in ListDict) {
            long InvCnt = DeriveInvCnt(EachPair.Value);
            //Console.WriteLine("色{0}の転倒数={1}", EachPair.Key, InvCnt);
            Answer -= InvCnt;
        }
        Console.WriteLine(Answer);
    }

    ////////////////////////////////////////////////////////////////
    // 列挙を引数として、列挙内での転倒数を返す
    ////////////////////////////////////////////////////////////////
    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 (int 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


解説

R G B G B
3 2 1 2 1
という例で考えます。

色が全部違う場合の転倒数は、
フェニック木を使って求めることができます。

求まった転倒数のなかで、同じ色であることによって
コストがかからない移動の回数は、
同じ色での転倒数になるので

色ごとの、同じ色での転倒数を引くことで解が分かります。