結果

問題 No.705 ゴミ拾い Hard
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-05-18 00:37:42
言語 Java19
(openjdk 21)
結果
AC  
実行時間 667 ms / 1,500 ms
コード長 4,486 bytes
コンパイル時間 2,549 ms
コンパイル使用メモリ 74,996 KB
実行使用メモリ 76,964 KB
最終ジャッジ日時 2023-09-10 22:45:28
合計ジャッジ時間 19,192 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,240 KB
testcase_01 AC 43 ms
49,260 KB
testcase_02 AC 43 ms
49,448 KB
testcase_03 AC 44 ms
49,292 KB
testcase_04 AC 44 ms
49,440 KB
testcase_05 AC 44 ms
49,292 KB
testcase_06 AC 44 ms
49,364 KB
testcase_07 AC 43 ms
47,368 KB
testcase_08 AC 43 ms
49,296 KB
testcase_09 AC 43 ms
49,524 KB
testcase_10 AC 43 ms
49,260 KB
testcase_11 AC 43 ms
49,304 KB
testcase_12 AC 44 ms
49,272 KB
testcase_13 AC 43 ms
49,312 KB
testcase_14 AC 71 ms
51,548 KB
testcase_15 AC 70 ms
49,764 KB
testcase_16 AC 70 ms
51,292 KB
testcase_17 AC 69 ms
51,336 KB
testcase_18 AC 71 ms
51,644 KB
testcase_19 AC 71 ms
51,724 KB
testcase_20 AC 75 ms
51,524 KB
testcase_21 AC 73 ms
51,808 KB
testcase_22 AC 69 ms
50,880 KB
testcase_23 AC 70 ms
51,532 KB
testcase_24 AC 610 ms
76,964 KB
testcase_25 AC 591 ms
74,608 KB
testcase_26 AC 631 ms
74,788 KB
testcase_27 AC 649 ms
76,748 KB
testcase_28 AC 637 ms
76,516 KB
testcase_29 AC 644 ms
75,068 KB
testcase_30 AC 612 ms
74,540 KB
testcase_31 AC 656 ms
73,708 KB
testcase_32 AC 636 ms
76,536 KB
testcase_33 AC 499 ms
74,056 KB
testcase_34 AC 474 ms
72,540 KB
testcase_35 AC 596 ms
70,216 KB
testcase_36 AC 667 ms
74,388 KB
testcase_37 AC 585 ms
74,040 KB
testcase_38 AC 603 ms
72,776 KB
testcase_39 AC 622 ms
72,696 KB
testcase_40 AC 44 ms
49,268 KB
testcase_41 AC 43 ms
49,448 KB
testcase_42 AC 43 ms
49,268 KB
testcase_43 AC 650 ms
73,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static long cube(long x){return x*x*x;}
    Main(){}
    long[] solveNaive(long[]a,long[]x,long[]y){
        int n=a.length;
        long[]dp=new long[n+1];
        Arrays.fill(dp,Long.MAX_VALUE);
        dp[0]=0;
        for(int i=0;i<n;++i){
            for(int j=0;j<=i;++j){
                dp[i+1]=Math.min(dp[i+1],
                                 dp[j]+cube(Math.abs(a[i]-x[j]))+cube(y[j]));
            }
        }
        return dp;
    }
    /*
     * i < j
     */
    int calculateReversePoint(long[]a,long[]x,long[]y,int i,int j,long[]dp){
        int n=a.length;
        int pass=n,fail=j-1;
        while(pass-fail>1){
            int middle=(pass+fail)/2;
            long value1=dp[i]+cube(Math.abs(a[middle]-x[i]))+cube(y[i]);
            long value2=dp[j]+cube(Math.abs(a[middle]-x[j]))+cube(y[j]);
            if(value1>=value2)pass=middle;
            else fail=middle;
        }
        return pass;
    }
    long[] solveMongeDP(long[]a,long[]x,long[]y){
        int n=a.length;
        long[]dp=new long[n+1];
        Arrays.fill(dp,Long.MAX_VALUE);
        dp[0]=0;
        int[]deq=new int[n+1];
        int[]rev=new int[n+1];
        int s=0,t=0;
        for(int i=0;i<n;++i){
            if(false){
                System.err.println("deq:");
                for(int j=s;j<t;++j){
                    System.err.print("("+deq[j]+","+rev[j]+") ");
                }
                System.err.println();
            }
            int pre=i;
            long value=dp[pre]+cube(Math.abs(a[i]-x[pre]))+cube(y[pre]);
            //System.err.println("C("+pre+","+(i+1)+")="+value);
            // TODO imperfect
            while(s<t){
                int oldPre=deq[t-1];
                int revPoint=calculateReversePoint(a,x,y,oldPre,i,dp);
                long oldValue=dp[oldPre]+cube(Math.abs(a[i]-x[oldPre]))
                    +cube(y[oldPre]);
                //System.err.println("C("+oldPre+","+(i+1)+")="+oldValue);
                if(revPoint<=i)t--;
                else if(t>=s+2&&rev[t-2]>=revPoint)t--;
                else{
                    rev[t-1]=revPoint;
                    break;
                }
            }
            while(s<t){
                if(rev[s]<=i)s++;
                else break;
            }
            deq[t]=i;
            t++;
            pre=deq[s];
            value=dp[pre]+cube(Math.abs(a[i]-x[pre]))+cube(y[pre]);
            dp[i+1]=value;
        }
        return dp;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        long[]a=sc.nextLongArray(n);
        long[]x=sc.nextLongArray(n);
        long[]y=sc.nextLongArray(n);
        Main solver=new Main();
        //long[]dp=solver.solveNaive(a,x,y);
        long[]dpMonge=solver.solveMongeDP(a,x,y);
        //System.err.println(Arrays.toString(dp));
        //System.err.println(Arrays.toString(dpMonge));
        out.println(dpMonge[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;
        }
        long[] nextLongArray(int n){
            long[]r=new long[n];
            for(int i=0;i<n;++i)r[i]=nextLong();
            return r;
        }
    }
}
0