Northwestern European Regional Contest 2016 NWERC ,F题Free Weights(优先队列+模拟)

传送门:

Vjudge:https://vjudge.net/problem/Gym-101170F

CF: http://codeforces.com/gym/101170

The city of Bath is a noted olympic training ground—bringing local, national, and even international teams to practice. However, even the finest gymnasium falls victim to the cardinal sin. . .Weights put back in the wrong spots. All of the pairs of dumbbells sit in no particular order on the two racks, possibly even with some of them split between rows. Initially each row has an equal number of dumbbells, however, this being a well-funded professional gym, there is infinite space at either end of each to hold any additional weights. To move a dumbbell, you may either roll it to a free neighbouring space on the same row with almost no effort, or you may pick up and lift it to another free spot; this takes strength proportional to its weight. For each pair of dumbbells, both have the same unique weight. What is the heaviest of the weights that you need to be able to lift in order to put identical weights next to each other? Note that you may end up with different numbers of weights on each row after rearranging; this is fine.

Input

The input consists of:

• one line containing the integer n (1 ≤ n ≤ 10^6 ), the number of pairs;

• two lines, each containing n integers w1 . . . wn (1 ≤ wi ≤ 10^9 for each i), where wi is the mass of the weight i-th from the left along this row. Every weight in the input appears exactly twice.

Output

Output the weight of the heaviest dumbbell that must be moved, in order that all items can be paired up while lifting the smallest possible maximum weight.

Sample Input 1

5

2 1 8 2 8

9 9 4 1 4

Sample Output 1

2

Sample Input 2

8

7 7 15 15 2 2 4 4

5 5 3 3 9 9 1 1

Sample Output 2

0

题目大意:

输入N,下面有两行,每行N个数字,代表杠铃的重量,保证给定数字,如果出现,肯定会出现2次,即构成一对。

询问的是要必须移动的杠铃中重量最大的杠铃的重量做到:

要求每两个相同重量的杠铃要在同一行相邻,很像消消乐,当然8 2 2 8不行,而8 8 2 2 可以。每个杠铃可以插入两个杠铃中间,或者放在一行的最右端最左端都行。

竖着两个不算相邻,比如说:

5

1 2 3 4 5

1 2 3 4 5

这里的1和1不相邻。

比如说第一组样例:

5

2 1 8 2 8

9 9 4 1 4

必须得把1,2移动。但是可以不移动8这个。所以最大重量是2。

第二组样例则不需要移动,已经是OK的了。所以输出0。

猜你喜欢

转载自www.cnblogs.com/Esquecer/p/9478490.html