To solve this problem, we can use a binary search to find the minimal k that satisfies the condition. For each k, we build a bipartite graph where gifts are on one side and wrapping papers are on the other side. An edge exists between a gift and a wrapping paper if the wrapping paper can wrap the gift when scaled by k. Then, we check if there is a perfect matching in this graph, which ensures all gifts can be wrapped.
Below is the Java code that implements this solution:
java
import java.util.*;
import java.io.*;
public class Main {
static int n;
static long[] a, b, c, d;
static ArrayList<Integer>[] adj;
static int[] matchTo;
static boolean[] vis;
public static void main(String[] args) throws IOException {
// Use BufferedReader and BufferedWriter for fast IO
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
n = Integer.parseInt(br.readLine());
a = new long[n];
b = new long[n];
c = new long[n];
d = new long[n];
for (int i = 0; i < n; i++) {
String[] tokens = br.readLine().split(" ");
a[i] = Long.parseLong(tokens[0]);
b[i] = Long.parseLong(tokens[1]);
c[i] = Long.parseLong(tokens[2]);
d[i] = Long.parseLong(tokens[3]);
}
// Binary search over k
long left = 1, right = (long) 2e9;
long ans = right;
while (left <= right) {
long mid = (left + right) / 2;
if (canWrapAllGifts(mid)) {
ans = mid; // possible to wrap all gifts with k = mid
right = mid - 1;
} else {
left = mid + 1;
}
}
out.println(ans);
out.flush();
}
static boolean canWrapAllGifts(long k) {
// Build adjacency list
adj = new ArrayList[n]; // gifts to wrapping papers
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
// For each gift i, find wrapping papers that can wrap it
for (int i = 0; i < n; i++) {
long giftLen = a[i];
long giftWid = b[i];
for (int j = 0; j < n; j++) {
long wrapLen = c[j] * k;
long wrapWid = d[j] * k;
if (wrapLen >= giftLen && wrapWid >= giftWid) {
adj[i].add(j);
}
}
}
// Initialize matching arrays
matchTo = new int[n]; // wrapping paper j is matched to gift matchTo[j]
Arrays.fill(matchTo, -1);
int matchCount = 0;
// For each gift, try to find an augmenting path
for (int i = 0; i < n; i++) {
vis = new boolean[n];
if (dfs(i)) {
matchCount++;
}
}
// Check if we have a perfect matching
return matchCount == n;
}
static boolean dfs(int u) {
vis[u] = true;
for (int v : adj[u]) {
if (matchTo[v] == -1 || (!vis[matchTo[v]] && dfs(matchTo[v]))) {
matchTo[v] = u;
return true;
}
}
return false;
}
}
Explanation of the code:
We use a binary search to find the minimal k.
For each k, we build a bipartite graph where nodes represent gifts and wrapping papers.
We add an edge between a gift and a wrapping paper if the wrapping paper can wrap the gift when scaled by k.
We attempt to find a maximum matching using DFS. If we can match all gifts with wrapping papers, then the current k is valid.
We adjust our binary search based on whether we can find a perfect matching or not.
We ensure all calculations use long to prevent integer overflows.
The code reads the input, processes it, and outputs the minimal k.
Now, let's test the code with the sample input provided:
Sample Input:
code
2
1 1 1 2
2 4 1 1
Sample Output:
code
2
Explanation:
With k = 2, we can scale the wrapping papers to sizes (1*2, 2*2) = (2,4) and (1*2, 1*2) = (2,2).
Both gifts can be wrapped by these papers.
With k = 1, it's not possible to wrap both gifts.
Therefore, the minimal k is 2.
The provided Java code correctly produces 2 when given this sample input.