square869120Contest    次のsquare869120Contestの問題へ    前のsquare869120Contestの問題へ

square869120コンテスト4 B問題 Buildings are Colorful!


問題へのリンク


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 5");
            WillReturn.Add("3949 3774 3598 3469 3424");
            //1541
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 3");
            WillReturn.Add("7 4 2 6 4");
            //7
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mK;
    static long[] mAArr;

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        mK = wkArr[1];

        mAArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        long Result = Solve(mK, mAArr);
        Console.WriteLine(Result);
    }

    static long Solve(long pK, long[] pAArr)
    {
        var IndList = new List<long>();
        for (long I = 0; I <= pAArr.GetUpperBound(0); I++) {
            IndList.Add(I);
        }

        long Answer = long.MaxValue;
        IEnumerable<long[]> IndArrEnum = RubyPatternClass<long>.Combination(IndList, pK);
        foreach (long[] EachIndArr in IndArrEnum) {
            // 添字0は必ず含む必要あり
            if (Array.IndexOf(EachIndArr, 0) < 0) continue;

            var HeightList = pAArr.ToList();
            long RunMax = HeightList[0];
            long CurrAnswer = 0;

            for (int I = 1; I <= HeightList.Count - 1; I++) {
                if (Array.IndexOf(EachIndArr, I) >= 0) {
                    long NeedHeight = RunMax + 1;
                    if (HeightList[I] < NeedHeight) {
                        CurrAnswer += NeedHeight - HeightList[I];
                        HeightList[I] = NeedHeight;
                    }
                }
                RunMax = Math.Max(RunMax, HeightList[I]);
            }
            Answer = Math.Min(Answer, CurrAnswer);
        }
        return Answer;
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 組合せを返す
    private struct JyoutaiDef_Combination
    {
        internal long CurrP;
        internal List<long> SelectedIndList;
    }
    internal static IEnumerable<Type[]> Combination(IEnumerable<Type> pEnum, long pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();
        if (pArr.Length < pR) yield break;

        var Stk = new Stack<JyoutaiDef_Combination>();
        JyoutaiDef_Combination WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.CurrP = I;
            WillPush.SelectedIndList = new List<long>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_Combination Popped = Stk.Pop();

            // クリア判定
            if (Popped.SelectedIndList.Count == pR) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();
                continue;
            }

            for (int I = pArr.GetUpperBound(0); Popped.CurrP + 1 <= I; I--) {
                WillPush.CurrP = I;
                WillPush.SelectedIndList = new List<long>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

見えるようにする建物を全探索してます。