No.2114 01 Matching
Problem Statement
There is a graph $G$ with $N+M$ vertices, consisting of $N$ blue vertices and $M$ red vertices. Initially, $G$ has no edges.
An integer is written on each vertex: the integer written on the $i$-th $(1\leq i \leq N)$ blue vertex is $B_i$, and the integer written on the $j$-th $(1 \leq j \leq M)$ red vertex is $R_j$.
First, you may perform the following Operation $1$ any number of times.
- Pay a cost of $1$, choose $1$ of the $N+M$ vertices of $G$, and increase the value of the integer written on that vertex by $K$.
After that, you may perform the following Operation $2$ any number of times.
- Choose a blue vertex and a red vertex, $1$ of each, that both have degree $0$ and have equal integers written on them, and add $1$ undirected edge between them.
Your goal is to make the number of edges in $G$ equal to $\min(N,M)$.
Determine whether the goal is achievable, and if it is, find the minimum total cost that must be paid to achieve it.
Input
$N\ M\ K$ $B_1\ B_2\ \ldots\ B_N$ $R_1\ R_2\ \ldots\ R_M$
- $1\le N,M\le 2×10^5$
- $1\le K\le 10^9$
- $0\le B_i,R_i\le 10^9$
- All input values are integers.
Output
If the goal is achievable, output the minimum total cost that must be paid to achieve it.
If the goal cannot be achieved no matter how the operations are performed, output -1.
Sample
Sample 1
Input
2 3 1 4 5 2 3 6
Output
2
For example, you can perform Operation $1$ as follows.
- Pay a cost of $1$ and increase the integer written on the $2$-nd blue vertex by $1$.
- Pay a cost of $1$ and increase the integer written on the $2$-nd red vertex by $1$.
When Operation $1$ is finished, $B_1=4,B_2=6,R_1=2,R_2=4,R_3=6$ holds.
You can then perform Operation $2$ as follows.
- Add an undirected edge between the $1$-st blue vertex and the $2$-nd red vertex.
- Add an undirected edge between the $2$-nd blue vertex and the $3$-rd red vertex.
By performing Operation $1$ and Operation $2$ in this way, you can make the number of edges in $G$ equal to $\min(2,3)=2$ with a total cost of $2$.
There is no way to achieve the goal with a cost of $1$ or less, so the answer is $2$.
Sample 2
Input
1 1 2 0 1
Output
-1
The goal cannot be achieved no matter how the operations are performed.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
bayashiko