結果
問題 |
No.704 ゴミ拾い Medium
|
ユーザー |
![]() |
提出日時 | 2018-01-28 02:30:13 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 755 ms / 1,500 ms |
コード長 | 3,271 bytes |
コンパイル時間 | 2,733 ms |
コンパイル使用メモリ | 78,260 KB |
実行使用メモリ | 63,632 KB |
最終ジャッジ日時 | 2024-12-29 07:21:40 |
合計ジャッジ時間 | 22,640 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 |
ソースコード
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; } }