結果

問題 No.489 株に挑戦
ユーザー uafr_csuafr_cs
提出日時 2017-02-24 23:40:23
言語 Java19
(openjdk 21)
結果
AC  
実行時間 789 ms / 1,000 ms
コード長 2,897 bytes
コンパイル時間 4,142 ms
コンパイル使用メモリ 85,572 KB
実行使用メモリ 67,096 KB
最終ジャッジ日時 2023-09-27 05:12:01
合計ジャッジ時間 21,544 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 656 ms
61,892 KB
testcase_01 AC 523 ms
60,864 KB
testcase_02 AC 156 ms
56,732 KB
testcase_03 AC 158 ms
56,728 KB
testcase_04 AC 129 ms
55,860 KB
testcase_05 AC 161 ms
56,868 KB
testcase_06 AC 167 ms
56,604 KB
testcase_07 AC 167 ms
56,684 KB
testcase_08 AC 156 ms
56,972 KB
testcase_09 AC 169 ms
56,824 KB
testcase_10 AC 168 ms
56,768 KB
testcase_11 AC 168 ms
56,752 KB
testcase_12 AC 168 ms
56,668 KB
testcase_13 AC 166 ms
56,584 KB
testcase_14 AC 168 ms
56,832 KB
testcase_15 AC 266 ms
60,896 KB
testcase_16 AC 631 ms
64,136 KB
testcase_17 AC 320 ms
60,784 KB
testcase_18 AC 493 ms
60,828 KB
testcase_19 AC 478 ms
60,404 KB
testcase_20 AC 665 ms
63,760 KB
testcase_21 AC 409 ms
60,540 KB
testcase_22 AC 298 ms
60,412 KB
testcase_23 AC 543 ms
60,880 KB
testcase_24 AC 770 ms
64,820 KB
testcase_25 AC 135 ms
55,736 KB
testcase_26 AC 649 ms
60,904 KB
testcase_27 AC 695 ms
64,028 KB
testcase_28 AC 154 ms
56,804 KB
testcase_29 AC 126 ms
55,692 KB
testcase_30 AC 789 ms
64,860 KB
testcase_31 AC 744 ms
67,096 KB
testcase_32 AC 556 ms
60,956 KB
testcase_33 AC 690 ms
65,020 KB
testcase_34 AC 668 ms
63,696 KB
testcase_35 AC 459 ms
60,696 KB
testcase_36 AC 348 ms
60,456 KB
testcase_37 AC 537 ms
61,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static class MaxSegTree{
		int n;
		long[] dat;
		int[] index;

		public MaxSegTree(int n_) {
			int n = 1;
			while(n < n_){ n *= 2; }

			this.n = n;
			dat = new long[this.n * 2 - 1];
			index = new int[this.n * 2 - 1];
			
			for(int i = 0; i < this.n * 2 - 1 ; i++){
				dat[i] = 0;
				index[i] = i;
			}
		}
		
		public void update(int k, long a){
			k += n - 1;
			dat[k] = a;
			index[k] = (k - (n - 1));

			while(k > 0){
				k = (k - 1) / 2;
				
				if(dat[k * 2 + 1] >= dat[k * 2 + 2]){
					dat[k] = dat[k * 2 + 1];
					index[k] = index[k * 2 + 1];
				}else{
					dat[k] = dat[k * 2 + 2];
					index[k] = index[k * 2 + 2];
				}
			}
		}
		
		public int query_index(int a, int b, int k, int l, int r){
			//System.out.println(a + " " + b + " " + k + " " + l + " " + r);
			if(r <= a || b <= l){ return -1; }
			if(a <= l && r <= b){ return index[k]; }
			
			final int left_index  = query_index(a, b, k * 2 + 1, l, (l + r) / 2);
			final int right_index = query_index(a, b, k * 2 + 2, (l + r) / 2, r);
			assert(left_index >= 0 || right_index >= 0);
			
			if(right_index < 0){ return left_index; }
			if(left_index < 0){ return right_index; }
			
			//System.out.println(dat[left_index] + " " + dat[right_index]);
			if(dat[left_index + (this.n - 1)] >= dat[right_index + (this.n - 1)]){
				return left_index;
			}else{
				return right_index;
			}
		}

		public int query_index(int a, int b){
			return query_index(a, b, 0, 0, n);
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int D = sc.nextInt();
		final int K = sc.nextInt();
		
		long[] price = new long[N];
		for(int i = 0; i < N; i++){
			price[i] = sc.nextLong();
		}
		
		MaxSegTree seg = new MaxSegTree(N);
		for(int i = 0; i < N; i++){ seg.update(i, price[i]); }
		//System.out.println(Arrays.toString(seg.index));
		
		long answer = Long.MIN_VALUE;
		int a_begin = -1, b_begin = -1;
		
		for(int begin = 0; begin < N; begin++){
			final int end = Math.min(N, begin + D + 1);
			
			//System.out.println(begin + " " + end);
			
			final int max_pos = seg.query_index(begin, end);
			
			final long range_max  = price[max_pos];
			final long begin_price = price[begin];
			final long money = (range_max - begin_price) * K;
			
			//System.out.printf("[%d, %d) => %d - %d = %d\n", begin, end, range_max , begin_price, money);
			
			if(answer < money){
				answer = money;
				a_begin = begin;
				b_begin = max_pos;
			}
		}
		
		System.out.println(answer);
		if(answer != 0){
			System.out.println(a_begin + " " + b_begin);
		}
	}
}
0