結果

問題 No.595 登山
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-11-10 23:13:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,755 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 75,108 KB
実行使用メモリ 82,380 KB
最終ジャッジ日時 2023-08-16 05:11:59
合計ジャッジ時間 20,669 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
62,568 KB
testcase_01 AC 114 ms
64,072 KB
testcase_02 AC 109 ms
62,760 KB
testcase_03 AC 112 ms
62,068 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 112 ms
62,508 KB
testcase_25 AC 112 ms
60,408 KB
testcase_26 WA -
testcase_27 AC 730 ms
77,928 KB
testcase_28 AC 743 ms
77,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
    static final long I=1L<<50;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=Integer.parseInt(scan.next());
        long p=Integer.parseInt(scan.next());
        long[] h=new long[n];
        for(int i=0;i<n;++i)
            h[i]=Integer.parseInt(scan.next());
        long[] l=new long[n],r=new long[n];
        l[0]=r[n-1]=0;
        for(int i=1;i<n;++i)
            l[i]=l[i-1]+Math.max(h[i]-h[i-1],0);
        for(int i=n-2;i>=0;--i)
            r[i]=r[i+1]+Math.max(h[i]-h[i+1],0);
        MaxSegmentTree s1=new MaxSegmentTree();
        MaxSegmentTree s2=new MaxSegmentTree();
        long[] dp=new long[n];
        dp[0]=0;
        s1.update(0,l[0]);
        s2.update(0,-r[0]);
        for(int i=1;i<n;++i){
            long m=l[i];
            m=Math.min(m,-s1.query(0,i)+l[i]+p);
            m=Math.min(m,-s2.query(0,i)-r[i]+p);
            dp[i]=m;
            s1.update(i,-m+l[i]);
            s2.update(i,-m-r[i]);
        }
        System.out.println(dp[n-1]);
    }
}
class MaxSegmentTree {
    static final int SIZE = 1 << 18;
    static final long INFINITY = (1L << 61) - 1;
    long[] seg;
    MaxSegmentTree() {
	this.seg = new long[2 * SIZE];
    }
    void update(int x, long value) {
	x += SIZE - 1;
	this.seg[x] = value;
	while (x > 0) {
	    x = (x - 1) / 2;
	    this.seg[x] = Math.max(this.seg[2 * x + 1], this.seg[2 * x + 2]);
	}
    }
    long query(int l, int r) {
	l += SIZE - 1;
	r += SIZE - 1;
        long y = -INFINITY;
	while (l < r) {
	    if ((l & 1) == 0) {
		y = Math.max(y, this.seg[l]);
	    }
	    if ((r & 1) == 0) {
		y = Math.max(y, this.seg[r - 1]);
	    }
	    l /= 2;
	    r = (r - 1) / 2;
	}
	return y;
    }
}
0