QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21170. 圍欄之內

Statistics

Farmer John 發現許多乳牛奇怪地患有廣場恐懼症(害怕大片開闊空間)。為讓牠們少怕放牧,他用垂直(南北)及水平(東西)圍欄把大田分成較小區域。

大田是角點 $(0,0)$ 和 $(A,B)$ 的矩形。FJ 在不同位置 $a_1 \ldots a_n$ ($0 < a_i < A$) 建 $n$ 條垂直圍欄 ($0 \leq n \leq 2000$),從 $(a_i, 0)$ 到 $(a_i, B)$。又在 $b_1 \ldots b_m$ ($0 < b_i < B$) 建 $m$ 條水平圍欄 ($0 \leq m \leq 2000$),從 $(0, b_i)$ 到 $(A, b_i)$。每條垂直圍欄穿過每條水平圍欄,共分出 $(n+1)(m+1)$ 個區域。

但 FJ 忘了建門,乳牛無法離開所在區域走遍田地!他想移除部分圍欄讓相鄰區域互通。他選一些相鄰區域對,移除它們間整段圍欄,使乳牛能經開口到達大田任意位置。

例如以下圍欄布局:

+---+--+
|   |  |
+---+--+
|   |  |  
|   |  |
+---+--+

可以這樣打開:

+---+--+
|      |  
+---+  +  
|      |  
|      |
+---+--+

求實現目標所需移除圍欄的最小總長度。

輸入格式

第一行包含 $A$、$B$、$n$、$m$ ($1 \leq A, B \leq 1,000,000,000$)。接下來 $n$ 行包含 $a_1 \ldots a_n$,然後 $m$ 行包含 $b_1 \ldots b_m$。

輸出格式

輸出所需移除的最小長度。可能超出標準 32 位元整數,因此可能需要 64 位元整數類型(如 C/C++ 的 long long)。

範例

輸入格式 1

15 15 5 2
2
5
10
6
4
11
3

輸出格式 1

44

題目作者:Brian Dean

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.