AtCoderのABC
   次のABCの問題へ
   前のABCの問題へ
ABC047-D 高橋君と見えざる手
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 2");
            WillReturn.Add("100 50 200");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 8");
            WillReturn.Add("50 30 40 10 20");
            //2
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 100");
            WillReturn.Add("7 10 4 5 9 3 6 8 2 1");
            //2
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }
    struct PairInfoDef
    {
        internal int BuyPrice;
        internal int SellPrice;
    }
    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        var PairInfoList = new List<PairInfoDef>();
        int MaxProfit = int.MinValue;
        int MinPrice = AArr[0];
        for (int I = 1; I <= AArr.GetUpperBound(0); I++) {
            if (MinPrice < AArr[I]) {
                int CurrProfit = AArr[I] - MinPrice;
                bool WillAdd = false;
                if (MaxProfit < CurrProfit) {
                    PairInfoList.Clear();
                    MaxProfit = CurrProfit;
                    WillAdd = true;
                }
                if (MaxProfit == CurrProfit) {
                    WillAdd = true;
                }
                if (WillAdd) {
                    PairInfoList.Add(
                        new PairInfoDef() { BuyPrice = MinPrice, SellPrice = AArr[I] });
                }
            }
            MinPrice = Math.Min(MinPrice, AArr[I]);
        }
        //Console.WriteLine("最大利益になるペア");
        //PairInfoList.ForEach(pX => Console.WriteLine("買値={0},売値={1}", pX.BuyPrice, pX.SellPrice));
        Console.WriteLine(PairInfoList.Count);
    }
}
解説
リンゴの価格を順に見ながら、
利益が最大になるペアをListに保存してます。