#P7628. Dance Partner Matching
Dance Partner Matching
Dance Partner Matching
There are \( N \) boys and \( N \) girls at a dance party, and their heights are known. Each person can dance with at most one partner. Every boy has a preference: either he wants to dance with a girl taller than him or with a girl shorter than him. Similarly, each girl either wishes to dance with a boy taller than her or with a boy shorter than her. Note that if a boy and a girl have the same height, they do not want to dance with each other.
A valid pair must satisfy both parties' preferences. In particular, if a boy wants a taller girl then his partner must be taller than him, and his partner must be a girl who wants a partner shorter than herself. On the other hand, if a boy wants a shorter girl then his partner must be shorter than him, and his partner must be a girl who wants a partner taller than herself.
Formally, let a boy with height \( b \) and preference \( P_b \) and a girl with height \( g \) and preference \( P_g \) form a pair. We define the preferences as follows:
- If \( P_b = 0 \), the boy wants a girl with \( g > b \). If \( P_b = 1 \), he wants a girl with \( g < b \).
- If \( P_g = 0 \), the girl wants a boy with \( b > g \). If \( P_g = 1 \), she wants a boy with \( b < g \).
Thus, the only valid pairings are when:
- Case 1: Boy has \( P_b = 0 \) and Girl has \( P_g = 1 \) with \( b < g \), or
- Case 2: Boy has \( P_b = 1 \) and Girl has \( P_g = 0 \) with \( b > g \).
Your task is to determine the maximum number of dance partner pairs that can be formed while respecting these conditions. Note that the matchings for the two cases are independent.
inputFormat
The input consists of 5 lines:
- An integer \( N \) representing the number of boys (and girls).
- \( N \) space-separated integers representing the heights of the boys.
- A string of length \( N \) where each character is either '0' or '1'. If the i-th character is '0', the i-th boy wants a taller girl (i.e. his partner's height must be greater than his). If it is '1', he wants a shorter girl.
- \( N \) space-separated integers representing the heights of the girls.
- A string of length \( N \) where each character is either '0' or '1'. If the i-th character is '0', the i-th girl wants a taller boy (i.e. her partner's height must be greater than hers). If it is '1', she wants a shorter boy.
outputFormat
Output a single integer — the maximum number of pairs that can be formed following the above conditions.
sample
3
150 160 170
010
155 165 175
101
2