結果

問題 No.489 株に挑戦
ユーザー 明智重蔵明智重蔵
提出日時 2022-12-29 20:19:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 431 ms / 1,000 ms
コード長 6,701 bytes
コンパイル時間 1,073 ms
コンパイル使用メモリ 111,232 KB
実行使用メモリ 55,680 KB
最終ジャッジ日時 2024-05-03 17:20:20
合計ジャッジ時間 8,851 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 307 ms
47,104 KB
testcase_01 AC 206 ms
40,192 KB
testcase_02 AC 38 ms
19,712 KB
testcase_03 AC 36 ms
19,584 KB
testcase_04 AC 34 ms
19,968 KB
testcase_05 AC 37 ms
19,840 KB
testcase_06 AC 38 ms
19,840 KB
testcase_07 AC 37 ms
19,712 KB
testcase_08 AC 41 ms
19,712 KB
testcase_09 AC 41 ms
19,456 KB
testcase_10 AC 41 ms
19,584 KB
testcase_11 AC 45 ms
19,584 KB
testcase_12 AC 42 ms
19,456 KB
testcase_13 AC 41 ms
19,584 KB
testcase_14 AC 37 ms
19,840 KB
testcase_15 AC 54 ms
24,448 KB
testcase_16 AC 326 ms
39,808 KB
testcase_17 AC 81 ms
26,368 KB
testcase_18 AC 204 ms
33,536 KB
testcase_19 AC 172 ms
32,256 KB
testcase_20 AC 345 ms
51,960 KB
testcase_21 AC 112 ms
31,488 KB
testcase_22 AC 65 ms
25,472 KB
testcase_23 AC 232 ms
41,344 KB
testcase_24 AC 404 ms
55,680 KB
testcase_25 AC 36 ms
19,840 KB
testcase_26 AC 384 ms
54,644 KB
testcase_27 AC 431 ms
40,004 KB
testcase_28 AC 45 ms
19,456 KB
testcase_29 AC 36 ms
19,712 KB
testcase_30 AC 400 ms
55,036 KB
testcase_31 AC 393 ms
54,632 KB
testcase_32 AC 234 ms
41,332 KB
testcase_33 AC 394 ms
53,552 KB
testcase_34 AC 341 ms
50,984 KB
testcase_35 AC 172 ms
34,816 KB
testcase_36 AC 91 ms
26,240 KB
testcase_37 AC 243 ms
39,928 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

// https://yukicoder.me/problems/no/489
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("7 2 1000");
            WillReturn.Add("1");
            WillReturn.Add("3");
            WillReturn.Add("5");
            WillReturn.Add("1");
            WillReturn.Add("4");
            WillReturn.Add("4");
            WillReturn.Add("7");
            //4000
            //0 2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 4 100");
            WillReturn.Add("1");
            WillReturn.Add("2");
            WillReturn.Add("1");
            WillReturn.Add("2");
            WillReturn.Add("1");
            //100
            //0 1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 1 100");
            WillReturn.Add("5");
            WillReturn.Add("4");
            WillReturn.Add("3");
            WillReturn.Add("3");
            WillReturn.Add("1");
            //0
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ProfitInfoDef
    {
        internal int BuyInd;
        internal int SellInd;
        internal long Profit;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int N = wkArr[0];
        int D = wkArr[1];
        int K = wkArr[2];

        int[] XArr = InputList.Skip(1).Take(N).Select(pX => int.Parse(pX)).ToArray();
        int UB = XArr.GetUpperBound(0);

        var InsSparseTable = new SparseTable<int>(XArr, false);

        // IndのList[値]なDict
        var IndListDict = new Dictionary<int, List<int>>();
        for (int I = 0; I <= UB; I++) {
            if (IndListDict.ContainsKey(XArr[I]) == false) {
                IndListDict[XArr[I]] = new List<int>();
            }
            IndListDict[XArr[I]].Add(I);
        }

        var AnswerKouho = new List<ProfitInfoDef>();
        for (int I = 0; I <= UB; I++) {
            int RangeSta = I;
            int RangeEnd = I + D;
            RangeEnd = Math.Min(UB, RangeEnd);

            int MaxVal = InsSparseTable.Query(RangeSta, RangeEnd);
            ProfitInfoDef WillAdd;
            WillAdd.BuyInd = I;

            int ResultNibunhou = ExecNibunhou_LowerBound(I, IndListDict[MaxVal]);
            WillAdd.SellInd = IndListDict[MaxVal][ResultNibunhou];
            WillAdd.Profit = (long)K * (XArr[WillAdd.SellInd] - XArr[WillAdd.BuyInd]);
            AnswerKouho.Add(WillAdd);
        }
        if (AnswerKouho.Exists(pX => pX.Profit > 0)) {
            ProfitInfoDef AnswerItem = AnswerKouho.OrderByDescending(pX => pX.Profit).First();
            Console.WriteLine(AnswerItem.Profit);
            Console.WriteLine("{0} {1}", AnswerItem.BuyInd, AnswerItem.SellInd);
        }
        else {
            Console.WriteLine(0);
        }
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static int ExecNibunhou_LowerBound(int pVal, List<int> pList)
    {
        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList.Last()) {
            return -1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] >= pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }
}

#region SparseTable
// スパーステーブル
internal class SparseTable<Type>
{
    private Type[] mInitArr;
    private int UB_0;
    private int UB_1;

    // 最小値か最大値[開始Ind,2の指数]なArr
    private Type[,] mMinOrMaxArr;

    // Log2の値[2べきな値] なDict
    static Dictionary<int, int> mLogDict = new Dictionary<int, int>();

    // 最小値を取得するか?
    private bool mIsMin = false;

    // コンストラクタ
    internal SparseTable(IEnumerable<Type> pInitEnum, bool pIsMin)
    {
        mIsMin = pIsMin;

        mInitArr = pInitEnum.ToArray();
        UB_0 = mInitArr.GetUpperBound(0);

        int Sisuu = 0;
        int Beki2 = 1;
        while (true) {
            mLogDict[Beki2] = Sisuu;
            if (Beki2 > mInitArr.Length) {
                break;
            }
            Beki2 *= 2;
            Sisuu++;
        }
        UB_1 = Sisuu;

        mMinOrMaxArr = new Type[UB_0 + 1, UB_1 + 1];

        // 長さ1の分を初期化
        for (int I = 0; I <= UB_0; I++) {
            mMinOrMaxArr[I, 0] = mInitArr[I];
        }

        for (int LoopLength = 2; LoopLength < int.MaxValue; LoopLength *= 2) {
            bool WillBreak = true;
            int HalfRange = LoopLength / 2;
            for (int I = 0; I <= UB_0; I++) {
                int StaInd1 = I;
                int EndInd1 = I + HalfRange - 1;
                int StaInd2 = EndInd1 + 1;
                int EndInd2 = StaInd2 + HalfRange - 1;

                if (EndInd2 > UB_0) break;

                var KouhoList = new List<Type>();
                KouhoList.Add(mMinOrMaxArr[I, mLogDict[HalfRange]]);
                KouhoList.Add(mMinOrMaxArr[StaInd2, mLogDict[HalfRange]]);

                if (mIsMin) {
                    mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Min();
                }
                else {
                    mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Max();
                }

                WillBreak = false;
            }
            if (WillBreak) break;
        }
    }

    // 閉区間 [L,R]の最小値または最大値を求める
    internal Type Query(int pL, int pR)
    {
        // 区間を内包する2べきを返す
        int Beki2 = 1;
        int Sisuu = 0;
        while (true) {
            int LeftRangeMax = pL + Beki2 - 1;
            int RightRangeMin = pR - Beki2 + 1;

            if (LeftRangeMax + 1 >= RightRangeMin) {
                break;
            }
            Beki2 *= 2;
            Sisuu++;
        }

        var KouhoList = new List<Type>();
        KouhoList.Add(mMinOrMaxArr[pL, Sisuu]);
        KouhoList.Add(mMinOrMaxArr[pR - Beki2 + 1, Sisuu]);

        if (mIsMin) {
            return KouhoList.Min();
        }
        return KouhoList.Max();
    }
}
#endregion
0