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

ARC156-A Non-Adjacent Flip


問題へのリンク


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("3");
            WillReturn.Add("101");
            WillReturn.Add("6");
            WillReturn.Add("101101");
            WillReturn.Add("5");
            WillReturn.Add("11111");
            WillReturn.Add("6");
            WillReturn.Add("000000");
            WillReturn.Add("30");
            WillReturn.Add("111011100110101100101000000111");
            //1
            //2
            //-1
            //0
            //8
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        for (int I = 2; I <= InputList.Count - 1; I += 2) {
            string S = InputList[I];

            int Result = Solve(S);
            Console.WriteLine(Result);
        }
    }

    static int Solve(string pS)
    {
        int OmoteCnt = pS.ToCharArray().Count(pX => pX == '1');
        if (OmoteCnt % 2 == 1) return -1;

        if (OmoteCnt == 2) {
            if (pS.Length == 2) {
                return -1;
            }
            if (pS.Length == 3) {
                if (pS == "011") return -1;
                if (pS == "101") return 1;
                if (pS == "110") return -1;
            }
            if (pS.Length == 4) {
                if (pS == "0011") return 2;
                if (pS == "0101") return 1;
                if (pS == "0110") return 3;
                if (pS == "1001") return 1;
                if (pS == "1010") return 1;
                if (pS == "1100") return 2;
            }
            if (pS.Contains("11")) {
                return 2;
            }
        }
        return OmoteCnt / 2;
    }
}


解説

表面になってるコインの数と
全部のコイン枚数の組み合わせで場合分けしてます。