Official
B - Job Assignment Editorial by en_translator
We can exhaustively search every pair of of indices of employees assigned to Work A and Work B.
The total time complexity of this solution is \(\Theta(N^2)\). Besides, one can also solve this problem in a total of \(O(N)\) time.
Sample Code (C++)
#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
std::vector<int> a(n);
std::vector<int> b(n);
for (int i = 0; i < n; i++) a[i] = ri(), b[i] = ri();
int res = 1000000000;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
res = std::min(res, i == j ? a[i] + b[j] : std::max(a[i], b[j]));
printf("%d\n", res);
return 0;
}
Sample Code (Python)
n = int(input())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n) :
a[i], b[i] = map(int, input().split())
res = 1000000000
for i in range(n) :
for j in range(n) :
res = min(res, a[i] + b[j] if i == j else max(a[i], b[j]))
print(res)
posted:
last update: