AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC100-D Patisserie ABC


問題へのリンク


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 3");
            WillReturn.Add("3 1 4");
            WillReturn.Add("1 5 9");
            WillReturn.Add("2 6 5");
            WillReturn.Add("3 5 8");
            WillReturn.Add("9 7 9");
            //56
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 3");
            WillReturn.Add("1 -2 3");
            WillReturn.Add("-4 5 -6");
            WillReturn.Add("7 -8 -9");
            WillReturn.Add("-10 11 -12");
            WillReturn.Add("13 -14 15");
            //54
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 5");
            WillReturn.Add("10 -80 21");
            WillReturn.Add("23 8 38");
            WillReturn.Add("-94 28 11");
            WillReturn.Add("-26 -2 18");
            WillReturn.Add("-69 72 79");
            WillReturn.Add("-26 -86 -54");
            WillReturn.Add("-72 -50 59");
            WillReturn.Add("21 65 -32");
            WillReturn.Add("40 -94 87");
            WillReturn.Add("-62 18 82");
            //638
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("3 2");
            WillReturn.Add("2000000000 -9000000000 4000000000");
            WillReturn.Add("7000000000 -5000000000 3000000000");
            WillReturn.Add("6000000000 -1000000000 8000000000");
            //30000000000
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct XYZinfoDef
    {
        internal long X;
        internal long Y;
        internal long Z;
    }

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

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

        SplitAct(InputList[0]);

        long M = wkArr[1];

        var XYZinfoList = new List<XYZinfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            XYZinfoDef WillAdd;
            WillAdd.X = wkArr[0];
            WillAdd.Y = wkArr[1];
            WillAdd.Z = wkArr[2];
            XYZinfoList.Add(WillAdd);
        }

        var Q1 = XYZinfoList.OrderBy(pX => (-pX.X) + (-pX.Y) + (-pX.Z));
        var Q2 = XYZinfoList.OrderBy(pX => (-pX.X) + (-pX.Y) + (+pX.Z));
        var Q3 = XYZinfoList.OrderBy(pX => (-pX.X) + (+pX.Y) + (-pX.Z));
        var Q4 = XYZinfoList.OrderBy(pX => (-pX.X) + (+pX.Y) + (+pX.Z));
        var Q5 = XYZinfoList.OrderBy(pX => (+pX.X) + (-pX.Y) + (-pX.Z));
        var Q6 = XYZinfoList.OrderBy(pX => (+pX.X) + (-pX.Y) + (+pX.Z));
        var Q7 = XYZinfoList.OrderBy(pX => (+pX.X) + (+pX.Y) + (-pX.Z));
        var Q8 = XYZinfoList.OrderBy(pX => (+pX.X) + (+pX.Y) + (+pX.Z));

        var AnswerKouho = new List<long>();
        AnswerKouho.Add(DeriveABSSum(Q1, M));
        AnswerKouho.Add(DeriveABSSum(Q2, M));
        AnswerKouho.Add(DeriveABSSum(Q3, M));
        AnswerKouho.Add(DeriveABSSum(Q4, M));
        AnswerKouho.Add(DeriveABSSum(Q5, M));
        AnswerKouho.Add(DeriveABSSum(Q6, M));
        AnswerKouho.Add(DeriveABSSum(Q7, M));
        AnswerKouho.Add(DeriveABSSum(Q8, M));
        Console.WriteLine(AnswerKouho.Max());
    }

    static long DeriveABSSum(IEnumerable<XYZinfoDef> pEnum, long pM)
    {
        long SumX = 0;
        long SumY = 0;
        long SumZ = 0;

        foreach (var EachXYZinfo in pEnum.Take((int)pM)) {
            SumX += EachXYZinfo.X;
            SumY += EachXYZinfo.Y;
            SumZ += EachXYZinfo.Z;
        }
        return Math.Abs(SumX) + Math.Abs(SumY) + Math.Abs(SumZ);
    }
}


解説

解は、
Xが正か負
Yが正か負
Zが正か負
のいずれかなので
2の3乗で、8通りのいずれかになるので

8通りでソートしてチェックしてます。