結果

問題 No.489 株に挑戦
ユーザー 37zigen37zigen
提出日時 2017-02-24 23:37:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 739 ms / 1,000 ms
コード長 2,250 bytes
コンパイル時間 5,133 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 72,144 KB
最終ジャッジ日時 2023-09-27 05:09:55
合計ジャッジ時間 18,945 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 609 ms
67,364 KB
testcase_01 AC 490 ms
63,240 KB
testcase_02 AC 133 ms
56,960 KB
testcase_03 AC 135 ms
56,536 KB
testcase_04 AC 104 ms
55,680 KB
testcase_05 AC 136 ms
57,112 KB
testcase_06 AC 142 ms
56,792 KB
testcase_07 AC 136 ms
54,792 KB
testcase_08 AC 136 ms
57,016 KB
testcase_09 AC 149 ms
56,896 KB
testcase_10 AC 147 ms
56,816 KB
testcase_11 AC 140 ms
56,916 KB
testcase_12 AC 137 ms
56,888 KB
testcase_13 AC 141 ms
57,108 KB
testcase_14 AC 136 ms
56,600 KB
testcase_15 AC 246 ms
62,016 KB
testcase_16 AC 603 ms
69,172 KB
testcase_17 AC 309 ms
60,800 KB
testcase_18 AC 473 ms
63,112 KB
testcase_19 AC 469 ms
63,016 KB
testcase_20 AC 702 ms
69,204 KB
testcase_21 AC 379 ms
60,832 KB
testcase_22 AC 281 ms
62,688 KB
testcase_23 AC 523 ms
63,708 KB
testcase_24 AC 739 ms
71,008 KB
testcase_25 AC 117 ms
55,692 KB
testcase_26 AC 641 ms
68,188 KB
testcase_27 AC 686 ms
72,144 KB
testcase_28 AC 134 ms
56,948 KB
testcase_29 AC 111 ms
55,860 KB
testcase_30 AC 734 ms
71,160 KB
testcase_31 AC 698 ms
71,868 KB
testcase_32 AC 539 ms
63,064 KB
testcase_33 AC 722 ms
70,872 KB
testcase_34 AC 658 ms
69,760 KB
testcase_35 AC 451 ms
63,300 KB
testcase_36 AC 314 ms
61,328 KB
testcase_37 AC 502 ms
63,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Q485 {
	static int N, D;
	static long K;
	static int[] x;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		D = sc.nextInt();
		K = sc.nextInt();
		x = new int[N];
		for (int i = 0; i < N; ++i) {
			x[i] = sc.nextInt();
		}
		RMQ rmq = new RMQ(N);
		for (int i = 0; i < N; ++i) {
			rmq.setVal(i, x[i], i);
		}
		long maxProfit = 0;
		int srcDay = -1, dstDay = -1;
		for (int i = 0; i < N - 1; ++i) {
			RMQ.State s = rmq.query(i + 1, i + 1 + D, 0, rmq.n, 0);
			if (maxProfit < (s.price - x[i]) * K) {
				maxProfit = (s.price - x[i]) * K;
				srcDay = i;
				dstDay = s.day;
			}
		}
		if (maxProfit == 0) {
			System.out.println(0);
		} else {
			System.out.println(maxProfit);
			System.out.println(srcDay + " " + dstDay);
		}
	}

	static class RMQ {
		class State implements Comparable<State> {
			int day;
			long price;

			public State(int day, long price) {
				this.day = day;
				this.price = price;
			}

			@Override
			public int compareTo(State o) {
				if (price != o.price) {
					return -Long.compare(price, o.price);
				} else {
					return Long.compare(day, o.day);
				}
			}
		}

		int n;
		State[] val;
		long INF = Integer.MAX_VALUE / 10;

		public RMQ(int n_) {
			n = 1;
			while (n < n_)
				n *= 2;
			val = new State[2 * n - 1];
			Arrays.fill(val, new State((int) INF, -INF));
		}

		void setVal(int k, int price, int day) {
			k += n - 1;
			val[k] = new State(day, price);
			while (k > 0) {
				k = (k - 1) / 2;
				val[k] = max(val[2 * k + 1], val[2 * k + 2]);
			}
		}

		State query(int a, int b, int l, int r, int k) {
			if (b <= l || a >= r)
				return new State((int) INF, -INF);
			if (a <= l && r <= b)
				return val[k];
			else {
				State vl = query(a, b, l, (l + r) / 2, 2 * k + 1);
				State vr = query(a, b, (l + r) / 2, r, 2 * k + 2);
				return max(vl, vr);
			}
		}

		State max(State s1, State s2) {
			if (s1.compareTo(s2) < 0) {
				return new State(s1.day, s1.price);
			} else {
				return new State(s2.day, s2.price);
			}
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0