#C8083. Maximum Safe Crossing Pairs
Maximum Safe Crossing Pairs
Maximum Safe Crossing Pairs
You are given N citizens with their respective weights and M bridges, each bridge having a weight limit. The task is to form pairs of citizens such that the sum of their weights does not exceed the weight limit of one of the bridges. Each citizen can be paired only once and each bridge can be used for at most one pair. Your goal is to determine the maximum number of pairs that can safely cross the river using the available bridges.
Note: The solution involves sorting both the citizens’ weights and the bridges’ weight limits and then using a two-pointer approach to form valid pairs. The formulas used in the solution can be expressed as:
\( \text{if } w_i + w_j \leq L_k \text{ then the pair } (i,j) \text{ is valid} \)
inputFormat
The input is provided from standard input (stdin
) in the following format:
N M w1 w2 ... wN L1 L2 ... LM
Where:
N
is the number of citizens.M
is the number of bridges.w1, w2, ..., wN
represent the weights of the citizens.L1, L2, ..., LM
represent the weight limits of the bridges.
outputFormat
Output to stdout
a single integer which represents the maximum number of pairs of citizens that can safely cross the river.
6 3
10 20 30 40 50 60
50 90 110
2
</p>