結果

問題 No.1650 Moving Coins
ユーザー merlin
提出日時 2021-08-20 22:01:23
言語 Java
(openjdk 23)
結果
AC  
実行時間 951 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 2,763 ms
コンパイル使用メモリ 85,900 KB
実行使用メモリ 74,292 KB
最終ジャッジ日時 2024-10-14 03:40:24
合計ジャッジ時間 20,302 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

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));
        PrintWriter pw=new PrintWriter(System.out);
        int n=Integer.parseInt(bu.readLine());
        String s[]=bu.readLine().split(" ");
        int a[]=new int[n],b[]=new int[n],i;
        for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
        s=bu.readLine().split(" ");
        for(i=0;i<n;i++) b[i]=Integer.parseInt(s[i]);

        ArrayList<Integer> fin=new ArrayList<>();
        Deque<Integer> q=new LinkedList<>();
        int ans=0;
        for(i=0;i<n;i++) ans+=Math.abs(b[i]-a[i]);

        for(i=0;i<n;i++)
        {
            int j=i+1;
            q.add(i);
            while(j<n && ((a[i]<b[i] && a[j]<b[j] && a[j]<=b[j-1]) || (a[i]>b[i] && a[j]>b[j] && b[j]<=a[j-1])))
            q.addLast(j++);

            if(a[i]<=b[i])
            while(!q.isEmpty()) fin.add(q.removeLast());
            else while(!q.isEmpty()) fin.add(q.removeFirst());
            i=j-1;
        }

        pw.append(ans+"\n");
        for(int x:fin)
        {
            int l=a[x],r=b[x]; char c='R';
            if(l>r)
            {
                l=b[x]; r=a[x];
                c='L';
            }

            while(l++<r) pw.append((x+1)+" "+c+"\n");
        }
        pw.close();
    }
}
0