結果
| 問題 |
No.704 ゴミ拾い Medium
|
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2018-01-28 01:55:16 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 728 ms / 1,500 ms |
| コード長 | 3,584 bytes |
| コンパイル時間 | 3,134 ms |
| コンパイル使用メモリ | 78,528 KB |
| 実行使用メモリ | 72,764 KB |
| 最終ジャッジ日時 | 2024-12-29 07:19:24 |
| 合計ジャッジ時間 | 24,351 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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);
MinSegmentTree m1=new MinSegmentTree();
MinSegmentTree p1=new MinSegmentTree();
long[]dp=new long[n+1];
for(int i=0;i<=n;++i){
// update
if(i<n){
m1.update(x[i],Math.min(m1.query(x[i],x[i]+1),x[i]+y[i]+dp[i]));
p1.update(x[i],Math.min(p1.query(x[i],x[i]+1),-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(curA,300001)-curA);
ans=Math.min(ans,p1.query(0,curA+1)+curA);
dp[i+1]=ans;
}
if(false){
System.err.print("m1:");
for(int j=0;j<10;++j){
System.err.print(" "+m1.query(j,j+1));
}
System.err.println();
System.err.print("p1:");
for(int j=0;j<10;++j){
System.err.print(" "+p1.query(j,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 MinSegmentTree {
static final int SIZE = 1 << 19;
static final long INFINITY = (1L << 62) - 1;
long[] seg;
MinSegmentTree() {
this.seg = new long[2 * SIZE];
Arrays.fill(this.seg,INFINITY);
}
void update(int x, long value) {
x += SIZE - 1;
this.seg[x] = value;
while (x > 0) {
x = (x - 1) / 2;
this.seg[x] = Math.min(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.min(y, this.seg[l]);
}
if ((r & 1) == 0) {
y = Math.min(y, this.seg[r - 1]);
}
l /= 2;
r = (r - 1) / 2;
}
return y;
}
}
夕叢霧香(ゆうむらきりか)