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

ABC264-D "redocta".swap(i,i+1)


問題へのリンク


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("catredo");
            //8
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("atcoder");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("redocta");
            //21
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[0];

        var ValList = new List<long>();
        foreach (char EachChar in S) {
            if (EachChar == 'a') ValList.Add(1);
            if (EachChar == 't') ValList.Add(2);
            if (EachChar == 'c') ValList.Add(3);
            if (EachChar == 'o') ValList.Add(4);
            if (EachChar == 'd') ValList.Add(5);
            if (EachChar == 'e') ValList.Add(6);
            if (EachChar == 'r') ValList.Add(7);
        }

        Console.WriteLine(DeriveInvCnt(ValList));
    }

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


解説

バブルソートの交換回数なので
atcoder の各文字を 1,2,3,4,5,6,7
として、転倒数を求めてます。