結果
問題 | No.489 株に挑戦 |
ユーザー | uafr_cs |
提出日時 | 2017-02-24 23:40:23 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 820 ms / 1,000 ms |
コード長 | 2,897 bytes |
コンパイル時間 | 3,217 ms |
コンパイル使用メモリ | 90,448 KB |
実行使用メモリ | 60,664 KB |
最終ジャッジ日時 | 2024-07-19 23:11:07 |
合計ジャッジ時間 | 21,615 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
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); } } }