atcoder#ARC114D. [ARC114D] Moving Pieces on Line
[ARC114D] Moving Pieces on Line
Score : points
Problem Statement
Let . We have a graph with a vertex for each integer , and an undirected edge connecting Vertex and Vertex (we will call it Edge ) for each .
All edges in this graph are initially painted red. Additionally, we have pieces, and the -th of them is on Vertex .
Maroon can repeat the following operation:
- Choose a piece. If that piece is on Vertex , move it to Vertex or Vertex . Then, if the traversed edge is now painted red, repaint it blue, and vice versa.
During the process, there may be multiple pieces on the same vertex.
Maroon wants to do this operation zero or more times to make the edges painted in his desired combination of colors. The desired combination of colors is represented by an even number and integers : it means that Edge is red for , blue for , , red for . More formally, for each odd number , Edge is blue for each such that ; all other edges are red.
Find the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print .
Constraints
- is even.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print .
2 2
2 -1
-2 2
4
As shown below, we can achieve the desired state in four operations, which is optimal since we need to repaint four edges.
The following is the initial situation. For convenience, we have omitted edges to the left of and the right of .
We move the piece on to to get the following:
Then, we move the piece on to to get the following:
Then, we move the piece on to to get the following:
Then, we move the piece on to to get the following, which is the desired state:
2 2
2 2
5 8
9
There can be multiple pieces on the same vertex already in the beginning.
3 4
1 3 5
0 2 4 6
-1
4 4
3 4 5 6
3 4 5 6
2