Yet Another Contest 4 P1 - Snap

View as PDF

Submit solution


Points: 5 (partial)
Time limit: 1.0s
Memory limit: 512M

Author:
Problem type

Josh and Nils are playing a variant of snap!

The game involves both Josh and Nils starting with N cards each, where each of the 2N total cards contains an integer between 1 and 10^9. Then, in each of the N rounds, Josh and Nils will remove the top card from their decks and place them face-up on the table at the same time. If the displayed cards are matching (have the same integer written on them), then whoever is first to yell "Snap!" receives one point.

Before the game, all 2N cards are in a pile, with the i-th card from the top having the integer a_i written on it. First, as an umpire, you will cut the cards by removing X (0 \le X < 2N) cards from the top of the pile and moving them to the bottom of the pile, whilst preserving the order of the removed cards. Note that it is possible that X = 0, in which case no cards are moved. Then, Josh's deck will consist of the first N cards of the pile in top-to-bottom order, and Nils' deck will consist of the next N cards of the pile in top-to-bottom order.

To make the game more exciting, you wish to cut the cards optimally by carefully choosing the value of X, such that the total number of rounds with matching cards is maximised. What is the maximum possible number of rounds with matching cards?

Constraints

1 \le N \le 10^6

1 \le a_i \le 10^9

Subtask 1 [50%]

1 \le N \le 1000

Subtask 2 [50%]

No additional constraints.

Input Specification

The first line of input contains a single integer N.

The second line of input contains 2N space-separated integers, a_1, a_2, \dots, a_{2N}.

Output Specification

On a single line, output the maximum possible number of rounds where the displayed cards have the same integers.

Sample Input

3
1 2 3 1 4 5

Sample Output

1

Explanation

Consider Josh choosing X = 2. After completing the cut, the pile of 2N cards would have the integers \{3, 1, 4, 5, 1, 2\} from top to bottom.

Then, Josh's deck would have the integers \{3, 1, 4\} from top to bottom, whilst Nils' deck would have the integers \{5, 1, 2\} from top to bottom.

During the first round, Josh places a card with the integer 3, and Nils places a card with the integer 5. These cards do not match.

During the second round, Josh places a card with the integer 1, and Nils places a card with the integer 1. These cards match.

During the third round, Josh places a card with the integer 4, and Nils places a card with the integer 2. These cards do not match.

In total, there is 1 round where the cards match. It can be shown that this is the maximum number of rounds for all possible values of X.


Comments

There are no comments at the moment.