AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC138-A Larger Score


問題へのリンク


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 2");
            WillReturn.Add("2 1 1 2");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 1");
            WillReturn.Add("3 2 1");
            //-1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("20 13");
            WillReturn.Add("90699850 344821203 373822335 437633059 534203117 523743511 568996900 694866636 683864672 836230375 751240939 942020833 865334948 142779837 22252499 197049878 303376519 366683358 545670804 580980054");
            //13
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct NumInfo
    {
        internal int Ind;
        internal int Num;
    }

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

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

        int UB = AArr.GetUpperBound(0);

        var NumInfoList = new List<NumInfo>();
        for (int I = 0; I <= UB; I++) {
            NumInfo WillAdd;
            WillAdd.Ind = I;
            WillAdd.Num = AArr[I];
            NumInfoList.Add(WillAdd);
        }
        NumInfoList = NumInfoList.OrderByDescending(pX => pX.Num).ThenBy(pX => pX.Ind).ToList();

        var AnswerKouhoList = new List<int>();
        int? LastChangeInd = null;
        foreach (NumInfo EachNumInfo in NumInfoList) {
            if (EachNumInfo.Ind < K) {
                if (LastChangeInd == null) {
                    continue;
                }
                AnswerKouhoList.Add(LastChangeInd.Value - EachNumInfo.Ind);
            }
            else {
                if (LastChangeInd == null) {
                    LastChangeInd = EachNumInfo.Ind;
                }
                else {
                    LastChangeInd = Math.Min(EachNumInfo.Ind, LastChangeInd.Value);
                }
            }
        }

        if (AnswerKouhoList.Count > 0) {
            Console.WriteLine(AnswerKouhoList.Min());
        }
        else {
            Console.WriteLine(-1);
        }
    }
}


解説

配列の添字と値を構造体のListに設定してから、
OrderBy 値 DESC , 添字 ASC で下記の要領で見てます。

添字がスコア集計の範囲なら、解を緩和。
添字がスコア集計の範囲外なら、解候補の移動先を更新。