結果

問題 No.1767 BLUE to RED
ユーザー merlinmerlin
提出日時 2021-11-26 23:01:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 539 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 74,960 KB
実行使用メモリ 80,004 KB
最終ジャッジ日時 2023-09-12 05:27:02
合計ジャッジ時間 11,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,232 KB
testcase_01 AC 45 ms
49,360 KB
testcase_02 AC 45 ms
49,668 KB
testcase_03 AC 46 ms
49,128 KB
testcase_04 AC 55 ms
49,404 KB
testcase_05 AC 58 ms
51,532 KB
testcase_06 AC 48 ms
49,164 KB
testcase_07 AC 60 ms
51,476 KB
testcase_08 AC 65 ms
51,456 KB
testcase_09 AC 352 ms
72,444 KB
testcase_10 AC 539 ms
78,588 KB
testcase_11 AC 431 ms
71,108 KB
testcase_12 AC 414 ms
76,232 KB
testcase_13 AC 393 ms
74,392 KB
testcase_14 AC 465 ms
79,664 KB
testcase_15 AC 456 ms
79,600 KB
testcase_16 AC 459 ms
79,988 KB
testcase_17 AC 457 ms
79,736 KB
testcase_18 AC 455 ms
79,692 KB
testcase_19 AC 461 ms
79,420 KB
testcase_20 AC 469 ms
79,948 KB
testcase_21 AC 461 ms
79,680 KB
testcase_22 AC 488 ms
79,616 KB
testcase_23 AC 461 ms
80,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]);
        int i,a[]=new int[n],b[]=new int[m];

        s=bu.readLine().split(" ");
        for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
        s=bu.readLine().split(" ");
        for(i=0;i<m;i++) b[i]=Integer.parseInt(s[i]);

        int ans,l=0;
        while(l<m && b[l]<a[0]) l++;
        ans=Math.max(a[0]-b[0],0);
        ans+=Math.max(b[m-1]-a[n-1],0);

        for(i=1;i<n;i++)
        {
            ArrayList<Integer> te=new ArrayList<>();
            while(l<m && b[l]<=a[i]) te.add(b[l++]);
            ans+=minimum(te,a[i-1],a[i]);
        }
        System.out.println(ans);
    }

    static int minimum(ArrayList<Integer> te,int l,int r)
    {
        //System.out.println(te+" "+l+" "+r);
        if(te.size()==0) return 0;
        int n=te.size(),ans,i;
        ans=r-te.get(0);
        for(i=0;i+1<n;i++) ans=Math.min(ans,te.get(i)-l+r-te.get(i+1));
        ans=Math.min(ans,te.get(n-1)-l);
        return ans;
    }
}
0