結果
問題 |
No.1650 Moving Coins
|
ユーザー |
![]() |
提出日時 | 2021-08-23 14:40:09 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 545 ms / 2,000 ms |
コード長 | 2,011 bytes |
コンパイル時間 | 4,191 ms |
コンパイル使用メモリ | 78,240 KB |
実行使用メモリ | 55,524 KB |
最終ジャッジ日時 | 2024-11-07 17:02:02 |
合計ジャッジ時間 | 16,075 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int[] org = new int[n]; for (int i = 0; i < n; i++) { org[i] = sc.nextInt(); } int[] next = new int[n]; int count = 0; for (int i = 0; i < n; i++) { next[i] = sc.nextInt(); count += Math.abs(org[i] - next[i]); } StringBuilder sb = new StringBuilder(); ArrayDeque<Integer> stack = new ArrayDeque<>(); for (int i = 0; i < n; i++) { if (org[i] < next[i] && i < n - 1 && org[i + 1] <= next[i]) { stack.push(i); } else { move(i + 1, org[i], next[i], sb); while (stack.size() > 0) { int idx = stack.pop(); move(idx + 1, org[idx], next[idx], sb); } } } System.out.println(count); System.out.print(sb); } static void move(int stone, int from, int to, StringBuilder sb) { if (from > to) { for (int i = 0; i < from - to; i++) { sb.append(stone).append(" L\n"); } } else { for (int i = 0; i < to - from; i++) { sb.append(stone).append(" R\n"); } } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }