#P6179. Bessie's Card Game
Bessie's Card Game
Bessie's Card Game
Bessie is a huge fan of card games. In this game, a deck of 2N cards numbered from 1 to 2N is divided between Bessie and her friend Elsie, with each receiving N cards. The game is played in N rounds. In each round, both players play one card, and the player who plays the higher-numbered card wins the round.
Bessie knows the exact order in which Elsie will play her cards. Given this information, determine the maximum number of rounds Bessie can win if she plays optimally. In an optimal strategy, during each round, Bessie should use the smallest card that is still greater than Elsie's card, if possible. If no such card exists, she should discard her smallest available card.
The key observation is that Bessie's winning strategy is unaffected by the order of rounds: it suffices to match each of Elsie's played cards with the smallest Bessie card that can beat it.
The formula for the solution can be understood as follows: Let S be the set of Bessie's cards (which is the complement of Elsie's cards in the set \(\{1,2,\dots,2N\}\)). Then for each card \(e\) played by Elsie, Bessie can secure a win if there exists a card \(b \in S\) such that \(b > e\). The optimal number of wins is the maximum number of pairs \((e, b)\) with \(b > e\) and each card used only once.
inputFormat
The input consists of multiple lines. The first line contains a single integer N indicating the number of rounds (and the number of cards each player receives). The following N lines each contain an integer representing the card Elsie will play in that round.
It is guaranteed that the deck consists of the numbers from 1 to 2N, and Elsie's cards are distinct.
outputFormat
Output a single integer which is the maximum number of rounds Bessie can win.
sample
3
1
6
4
2