QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB

#21385. 回廊の調律

Statistics

回廊に沿って $n$ 個の共振器が順番に並んでいます。$i$ 番目の共振器の現在の周波数オフセットは $a_i$ であり、目標のオフセットは $b_i$ です。

調律装置は回廊のどちらか一端からのみ起動できます。$1$ 回の操作では、任意の $0$ でない整数 $x$ を選び、次のいずれかの操作を行うことができます:

  1. $k$($1 \le k \le n$)を選び、接頭辞 $a_1, a_2, \ldots, a_k$ のすべての数に $x$ を加える。
  2. $k$($1 \le k \le n$)を選び、接尾辞 $a_k, a_{k+1}, \ldots, a_n$ のすべての数に $x$ を加える。

$x$ は正でも負でも構いません。$|x|$ の大きさにかかわらず、この変更はちょうど $1$ 回の操作として数えられます。

数列 $a$ を数列 $b$ に変換するために必要な最小の操作回数を求めてください。

入力

最初の行には、テストケースの個数を表す整数 $T$($1 \le T \le 10^4$)が含まれます。

各テストケースについて:

  • $1$ 行目には整数 $n$($1 \le n \le 2 \times 10^5$)が含まれます。
  • $2$ 行目には $n$ 個の整数 $a_1, a_2, \ldots, a_n$($-10^9 \le a_i \le 10^9$)が含まれます。
  • $3$ 行目には $n$ 個の整数 $b_1, b_2, \ldots, b_n$($-10^9 \le b_i \le 10^9$)が含まれます。

すべてのテストケースにおける $n$ の総和は $2 \times 10^5$ を超えず、すべてのテストケースにわたる $\sum |a_i - a_{i-1}|$($i \ge 2$)および $\sum |b_i - b_{i-1}|$($i \ge 2$)の総和はそれぞれ $10^4$ を超えないことが保証されます。

出力

各テストケースについて、最小の操作回数を表す整数を $1$ 行ずつ出力してください。

入出力例

入力 1

4
3
0 0 0
-1 2 0
3
0 0 0
3 1 4
4
1 4 2 8
6 9 7 13
2
-7 10
-7 10

出力 1

2
3
1
0

注記

最初のテストケースでは、可能な操作手順の一つは次の通りです:

  1. 長さ $1$ の接頭辞に $-3$ を加え、$[-3, 0, 0]$ を得る。
  2. 長さ $2$ の接頭辞に $2$ を加え、$[-1, 2, 0]$ を得る。

$3$ 番目のテストケースでは、数列全体に $5$ を加えるだけで十分です。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.