Explanation
At a glance, it's difficult to directly compute the answer without comparing each pair of arrays. Instead of directly computing the answer, we can try binary searching on it.
If the value we are checking is , then we need a pair of arrays and where for all valid . We can rephrase this to where either or . Given that is small, this means that we can represent each array as being a bitmask, where the -th bit in a given bitmask is turned on if in our current array.
Because there are only a maximum of bits in our bitmask, there are up to possible bitmasks. Note that we can consider two arrays with the same bitmask as being functionally equivalent. Thus, we can brute force every pair of bitmasks to see if any pair of arrays satisfies the condition for the value we are checking for.
Implementation
Time Complexity: , where is the largest value in the array.
C++
#include <bits/stdc++.h>using ll = long long;int main() {int n, m;std::cin >> n >> m;std::vector vals(n, std::vector<int>(m));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) { std::cin >> vals[i][j]; }
Java
import java.io.*;import java.util.*;public class Minimax {static int n, m;static int[][] vals;static int chosenA = 0, chosenB = 0;public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Python
Warning!
This solution only runs in time when submitted with PyPy.
import sysinput = sys.stdin.readlinen, m = map(int, input().split())vals = [list(map(int, input().split())) for _ in range(n)]chosen = [0, 0]def check(res: int) -> bool:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!