結果
問題 |
No.489 株に挑戦
|
ユーザー |
|
提出日時 | 2017-02-25 00:29:23 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 665 ms / 1,000 ms |
コード長 | 1,764 bytes |
コンパイル時間 | 3,965 ms |
コンパイル使用メモリ | 84,580 KB |
実行使用メモリ | 49,964 KB |
最終ジャッジ日時 | 2024-07-19 23:22:55 |
合計ジャッジ時間 | 18,026 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
package yukicoder; import java.awt.List; import java.util.ArrayDeque; 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(); } MonotonicPriorityQueue mpq = new MonotonicPriorityQueue(); for (int i = 1; i <= Math.min(D, N); ++i) { mpq.push(x[i], i); } long maxProfit = 0; long srcDay = -1; long dstDay = -1; for (int i = 0; i + 1 < N; ++i) { if (maxProfit < (mpq.pop().val - x[i]) * K) { maxProfit = (mpq.pop().val - x[i]) * K; srcDay = i; dstDay = mpq.pop().day; } mpq.poll(); if (i + D + 1 < N) mpq.push(x[i + D + 1], i + D + 1); } if (maxProfit > 0) { System.out.println(maxProfit); System.out.println(srcDay + " " + dstDay); } else { System.out.println(0); } } static class MonotonicPriorityQueue { class Pair { long val, day, cnt; public Pair(long val, long cnt, long day) { this.val = val; this.cnt = cnt; this.day = day; } } ArrayDeque<Pair> que = new ArrayDeque<>(); void push(int val, long day) { int cnt = 0; while (!que.isEmpty() && que.getLast().val < val) { cnt += que.getLast().cnt + 1; que.pollLast(); } que.addLast(new Pair(val, cnt, day)); } Pair pop() { return que.getFirst(); } Pair poll() { Pair ret = pop(); if (que.getFirst().cnt > 0) { --que.getFirst().cnt; } else { que.pollFirst(); } return ret; } } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }