結果

問題 No.704 ゴミ拾い Medium
ユーザー 夕叢霧香(ゆうむらきりか)
提出日時 2018-01-28 02:49:01
言語 Java
(openjdk 23)
結果
AC  
実行時間 622 ms / 1,500 ms
コード長 2,734 bytes
コンパイル時間 2,660 ms
コンパイル使用メモリ 77,884 KB
実行使用メモリ 63,748 KB
最終ジャッジ日時 2024-12-29 07:24:04
合計ジャッジ時間 19,483 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

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();
        int[]dp=new int[n+1];
        for(int i=0;i<n;++i){
            // update
            m1.update(300001-x[i],x[i]+y[i]+dp[i]);
            p1.update(1+x[i],-x[i]+y[i]+dp[i]);
            // query
            int ans=Integer.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;
        }
        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 int INFINITY = (1 << 30) - 1;
    int[] bit;
    MinBIT() {
	this.bit = new int[SIZE];
        Arrays.fill(this.bit,INFINITY);
    }
    void update(int x, int value) {
        while(x<SIZE){
            bit[x]=Math.min(bit[x],value);
            x+=x&(-x);
        }
    }
    int query(int x) {
        int y = INFINITY;
	while(x>0){
            y=Math.min(y,bit[x]);
            x&=x-1;
	}
	return y;
    }
}
0