結果

問題 No.704 ゴミ拾い Medium
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-28 02:30:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 556 ms / 1,500 ms
コード長 3,271 bytes
コンパイル時間 2,467 ms
コンパイル使用メモリ 74,312 KB
実行使用メモリ 75,552 KB
最終ジャッジ日時 2023-08-28 10:14:05
合計ジャッジ時間 17,993 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
57,592 KB
testcase_01 AC 70 ms
57,428 KB
testcase_02 AC 58 ms
57,344 KB
testcase_03 AC 58 ms
57,384 KB
testcase_04 AC 58 ms
57,420 KB
testcase_05 AC 59 ms
57,428 KB
testcase_06 AC 59 ms
57,320 KB
testcase_07 AC 61 ms
57,304 KB
testcase_08 AC 58 ms
57,316 KB
testcase_09 AC 59 ms
57,320 KB
testcase_10 AC 70 ms
57,368 KB
testcase_11 AC 58 ms
57,332 KB
testcase_12 AC 59 ms
57,284 KB
testcase_13 AC 59 ms
57,564 KB
testcase_14 AC 86 ms
57,704 KB
testcase_15 AC 90 ms
57,776 KB
testcase_16 AC 93 ms
57,972 KB
testcase_17 AC 85 ms
57,976 KB
testcase_18 AC 91 ms
58,324 KB
testcase_19 AC 88 ms
57,808 KB
testcase_20 AC 86 ms
57,832 KB
testcase_21 AC 86 ms
58,176 KB
testcase_22 AC 90 ms
58,196 KB
testcase_23 AC 95 ms
58,156 KB
testcase_24 AC 540 ms
74,136 KB
testcase_25 AC 538 ms
72,724 KB
testcase_26 AC 537 ms
73,960 KB
testcase_27 AC 536 ms
73,064 KB
testcase_28 AC 501 ms
71,052 KB
testcase_29 AC 513 ms
72,788 KB
testcase_30 AC 527 ms
74,476 KB
testcase_31 AC 530 ms
74,596 KB
testcase_32 AC 508 ms
73,604 KB
testcase_33 AC 516 ms
74,072 KB
testcase_34 AC 532 ms
73,508 KB
testcase_35 AC 473 ms
71,584 KB
testcase_36 AC 538 ms
74,048 KB
testcase_37 AC 483 ms
69,616 KB
testcase_38 AC 514 ms
74,544 KB
testcase_39 AC 471 ms
71,788 KB
testcase_40 AC 478 ms
69,724 KB
testcase_41 AC 480 ms
73,604 KB
testcase_42 AC 477 ms
73,756 KB
testcase_43 AC 479 ms
71,608 KB
testcase_44 AC 57 ms
57,416 KB
testcase_45 AC 59 ms
57,304 KB
testcase_46 AC 536 ms
75,552 KB
testcase_47 AC 556 ms
71,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]a=sc.nextIntArray(n);
        int[]x=sc.nextIntArray(n);
        int[]y=sc.nextIntArray(n);
        MinBIT m1=new MinBIT();
        MinBIT p1=new MinBIT();
        long[]dp=new long[n+1];
        for(int i=0;i<=n;++i){
            // update
            if(i<n){
                m1.update(300001-x[i],x[i]+y[i]+dp[i]);
                p1.update(1+x[i],-x[i]+y[i]+dp[i]);
            }
            // query
            if(i<n){
                long ans=Long.MAX_VALUE;
                int curA=a[i];
                ans=Math.min(ans,m1.query(300001-curA)-curA);
                ans=Math.min(ans,p1.query(curA+1)+curA);
                dp[i+1]=ans;
            }
            if(false){
                System.err.print("m1-acc:");
                for(int j=0;j<10;++j){
                    System.err.print(" "+m1.query(j+1));
                }
                System.err.println();
                System.err.print("p1-acc:");
                for(int j=0;j<10;++j){
                    System.err.print(" "+p1.query(j+1));
                }
                System.err.println();
            }
        }
        out.println(dp[n]);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
class MinBIT {
    static final int SIZE = 1 << 19;
    static final long INFINITY = (1L << 62) - 1;
    long[] bit;
    MinBIT() {
	this.bit = new long[SIZE];
        Arrays.fill(this.bit,INFINITY);
    }
    void update(int x, long value) {
        while(x<SIZE){
            bit[x]=Math.min(bit[x],value);
            x+=x&(-x);
        }
    }
    long query(int x) {
        long y = INFINITY;
	while(x>0){
            y=Math.min(y,bit[x]);
            x&=x-1;
	}
	return y;
    }
}
0