AtCoderのAGC    次のAGCの問題へ    前のAGCの問題へ

AGC040-A ><


問題へのリンク


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

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

        S = S.Replace('<', '↑');
        S = S.Replace('>', '↓');

        // 先頭が↓だったら、先頭に↑を追加
        if (S.StartsWith("↓")) {
            S = "↑" + S;
        }
        int UB = S.Length - 1;

        int[] NumArr = new int[UB + 1];

        // 文字列を逆に走査し、↓の箇所に値を設定
        int SeqCnt = 0;
        for (int I = UB; 0 <= I; I--) {
            if (S[I] == '↓') {
                NumArr[I] = SeqCnt;
                SeqCnt++;
            }
            if (S[I] == '↑') {
                SeqCnt = 0;
            }
        }

        // 文字列を逆に走査し、↑の箇所に値を設定
        for (int I = 0; I <= UB; I++) {
            int PrevVal = 0;
            if (I > 0) {
                PrevVal = NumArr[I - 1];
            }

            if (S[I] == '↑') {
                var KouhoList = new List<int>();
                KouhoList.Add(PrevVal + 1);

                if (I < UB && S[I + 1] == '↓') {
                    KouhoList.Add(NumArr[I + 1] + 1);
                }
                NumArr[I] = KouhoList.Max();
            }
            //Console.WriteLine("S[{0}]={1}", I, NumArr[I]);
        }

        long Answer = 0;
        Array.ForEach(NumArr, pX => Answer += pX);
        Console.WriteLine(Answer);
    }
}


解説

単調減少のほうが決定しやすいので、先に単調減少の箇所の値を決定します。
次に、単調増加の箇所の値を決定します。