AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0525 おせんべい


問題へのリンク


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("2 5");
            WillReturn.Add("0 1 0 1 0");
            WillReturn.Add("1 0 0 0 1");
            WillReturn.Add("3 6");
            WillReturn.Add("1 0 0 0 1 0");
            WillReturn.Add("1 1 1 0 1 0");
            WillReturn.Add("1 0 1 1 0 1  ");
            WillReturn.Add("0 0");
            //9
            //15
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        for (int I = 0; I <= InputList.Count - 1; I++) {
            InputList[I] = InputList[I].TrimEnd();
        }

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int CurrInd = 0;
        while (true) {
            SplitAct(InputList[CurrInd]);
            int R = wkArr[0];
            int C = wkArr[1];
            if (R == 0 && C == 0) break;

            int[,] BanArr = CreateBanArr(InputList.Skip(CurrInd + 1).Take(R));
            Solve(BanArr, R);

            CurrInd += R + 1;
        }
    }

    static void Solve(int[,] pBanArr, int pR)
    {
        int UB_X = pBanArr.GetUpperBound(0);
        int UB_Y = pBanArr.GetUpperBound(1);

        // 縦の値のListを作成
        var RValList = new List<int>();
        for (int X = 0; X <= UB_X; X++) {
            int Beki2 = 1;
            int CurrBit = 0;
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (pBanArr[X, Y] == 1) {
                    CurrBit += Beki2;
                }
                Beki2 *= 2;
            }
            RValList.Add(CurrBit);
        }

        // 縦の全部の01の組み合わせのListを作成
        var RBitSetList = new List<int>();
        var BaseList = new List<int>() { 0, 1 };
        foreach (int[] EachArr in RubyPatternClass<int>.RepeatedPermutation(BaseList, pR)) {
            int Beki2 = 1;
            int CurrBit = 0;
            foreach (int EachVal in EachArr) {
                if (EachVal == 1) CurrBit += Beki2;
                Beki2 *= 2;
            }
            RBitSetList.Add(CurrBit);
        }

        var AnswerList = new List<int>();
        foreach (int EachRBitSet in RBitSetList) {
            int AnswerKouho = 0;
            foreach (int EachRVal in RValList) {
                int CurrVal = EachRVal ^ EachRBitSet;
                int PopCnt1 = PopCount(CurrVal);
                int PopCnt2 = pR - PopCnt1;
                AnswerKouho += Math.Max(PopCnt1, PopCnt2);
            }
            AnswerList.Add(AnswerKouho);
        }
        Console.WriteLine(AnswerList.Max());
    }

    ////////////////////////////////////////////////////////////////
    // C++のPopCount
    ////////////////////////////////////////////////////////////////
    static int PopCount(int pVal)
    {
        int WillReturn = 0;
        while (pVal > 0) {
            if (pVal % 2 == 1) WillReturn++;
            pVal /= 2;
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

        int[] IntArr = { };
        Action<string> SplitAct = pStr =>
            IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 重複順列を返す
    private struct JyoutaiDef_RepeatedPermutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> RepeatedPermutation(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();

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

        while (Stk.Count > 0) {
            JyoutaiDef_RepeatedPermutation 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); 0 <= I; I--) {
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

縦の上限は10
横の上限は10000
なので、縦の01の組み合わせは、
2の10乗で32通りしかないです。

なので、縦の組み合わせごとの、
最適値を求めるようにしてます。