結果

問題 No.489 株に挑戦
ユーザー tentententen
提出日時 2020-12-24 16:21:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 691 ms / 1,000 ms
コード長 2,213 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 87,320 KB
実行使用メモリ 73,120 KB
最終ジャッジ日時 2023-10-21 15:43:45
合計ジャッジ時間 19,486 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 584 ms
62,412 KB
testcase_01 AC 478 ms
62,204 KB
testcase_02 AC 143 ms
58,012 KB
testcase_03 AC 144 ms
58,144 KB
testcase_04 AC 119 ms
57,188 KB
testcase_05 AC 144 ms
58,268 KB
testcase_06 AC 149 ms
58,356 KB
testcase_07 AC 152 ms
58,056 KB
testcase_08 AC 143 ms
58,212 KB
testcase_09 AC 151 ms
57,584 KB
testcase_10 AC 154 ms
56,296 KB
testcase_11 AC 154 ms
58,276 KB
testcase_12 AC 157 ms
58,004 KB
testcase_13 AC 141 ms
58,252 KB
testcase_14 AC 155 ms
58,036 KB
testcase_15 AC 226 ms
62,516 KB
testcase_16 AC 579 ms
66,936 KB
testcase_17 AC 312 ms
62,076 KB
testcase_18 AC 446 ms
61,940 KB
testcase_19 AC 470 ms
61,888 KB
testcase_20 AC 590 ms
67,184 KB
testcase_21 AC 379 ms
61,872 KB
testcase_22 AC 285 ms
62,032 KB
testcase_23 AC 505 ms
62,256 KB
testcase_24 AC 691 ms
71,260 KB
testcase_25 AC 133 ms
56,956 KB
testcase_26 AC 571 ms
65,288 KB
testcase_27 AC 644 ms
73,120 KB
testcase_28 AC 131 ms
57,564 KB
testcase_29 AC 112 ms
56,116 KB
testcase_30 AC 608 ms
67,820 KB
testcase_31 AC 600 ms
69,348 KB
testcase_32 AC 461 ms
62,172 KB
testcase_33 AC 615 ms
71,252 KB
testcase_34 AC 586 ms
68,056 KB
testcase_35 AC 412 ms
62,016 KB
testcase_36 AC 320 ms
61,912 KB
testcase_37 AC 479 ms
62,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int d = sc.nextInt();
		long k = sc.nextInt();
		int[] arr = new int[n];
		SegmentTree st = new SegmentTree(n);
		for (int i = 0; i < n; i++) {
		    arr[i] = sc.nextInt();
		    st.update(i, arr[i]);
		}
		long max = 0;
		int start = 0;
		int end = 0;
		for (int i = 1; i < n; i++) {
		    long tmp = st.query(Math.max(i - d, 0), i);
		    if (max < arr[i] - tmp) {
		        max = arr[i] - tmp;
		        end = i;
		        for (int j = Math.max(i - d, 0); j < i; j++) {
		            if (arr[j] == tmp) {
		                start = j;
		                break;
		            }
		        }
		    }
		    max = Math.max(max, arr[i] - tmp);
		}
		System.out.println(max * k);
		if (max > 0) {
		    System.out.println(start + " " + end);
		}
	}
}

class SegmentTree {
    static final int INF = Integer.MAX_VALUE;
    int size;
    int base;
    int[] tree;
    
    public SegmentTree(int size) {
        this.size = size;
        base = 1;
        while (base < size) {
            base <<= 1;
        }
        tree = new int[base * 2 - 1];
        Arrays.fill(tree, Integer.MAX_VALUE);
    }
    
    public void update(int idx, int value) {
        updateTree(idx + base - 1, value);
    }
    
    private void updateTree(int idx, int value) {
        tree[idx] = value;
        if (idx == 0) {
            return;
        }
        
        if (idx % 2 == 1) {
            updateTree((idx - 1) / 2, Math.min(value, tree[idx + 1]));
        } else {
            updateTree((idx - 1) / 2, Math.min(value, tree[idx - 1]));
        }
    }
    
    public int query(int min, int max) {
        return query(min, max, 0, 0, base);
    }
    
    public int query(int min, int max, int idx, int left, int right) {
        if (min >= right || max < left) {
            return INF;
        }
        if (min <= left && right - 1 <= max) {
            return tree[idx];
        }
        int x1 = query(min, max, 2 * idx + 1, left, (left + right) / 2);
        int x2 = query(min, max, 2 * idx + 2, (left + right) / 2, right);
        return Math.min(x1, x2);
    }
 }
0