Info/Tips
Oftentimes, problems will try to trick you. For example, since the judge is asking for a double, you would assume you may need to some advanced geometry or some diagonal movements, but a good rule of thumb is to always stay simple: the most elegant and more importantly the fastest solutions are the shortest.
Explanation
Let's call:
- and the leftmost and rightmost points of table 1 respectively
- and the bottommost and topmost points of table 1 respectively
- and the width and height of table 1 (equal to the difference in and and and respectively)
- and the width and height of table 2 respectively
- and the width and height of the room respectively
The key to this problem is realising that table 2 will only ever be in 4 different positions relative to table 1: to its left, right, top or bottom (see diagram below). In addition, in all cases we can assume that table 2 will be touching its respective wall in order to (attempt) to maximise its distance from table 1. E.g., if placing table 2 to the left of table 1, we will assume its leftmost edge will be touching the left wall of the room. Determining the solution is then simply a case of simulating all of these cases and finding which involves moving table 1 the smallest distance.
Possible placements of table 2 relative to table 1.
To determine the minimum distance that table 1 must be moved, we have to calculate the overlap between the two tables. For example:
In this case, table 2 is placed to the left of table 1. We can therefore calculate its overlap with table 1 as follows: - . Here the overlap is 1, meaning we must move table 1 one space from its starting position to fit table 2 in the room. We must then compare this answer to the answers we get from placing table 2 to the right, top or bottom of table 1. The smallest of all of these is our final answer.
To summarise, we calculate the overlap of the two tables as follows:
- Table 2 placed to the left of table 1: Overlap = -
- Table 2 placed to the right of table 1: Overlap = - ( - )
- Table 2 placed to the top of table 1: Overlap = - ( - )
- Table 2 placed to the bottom of table 1: Overlap = -
It is possible that the two tables don't overlap at all. In this case the above calculations would return a negative number so we should instead return 0 to indicate that table 1 doesn't have to move at all.
Warning!
When placing table 2 on the left and right sides, make sure , otherwise table 2 won't fit in the room. Similarly, when placing table 2 on the top and bottom sides, make sure , otherwise table 2 won't fit in the room.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {int tc;cin >> tc;for (int i = 0; i < tc; i++) {int total_width, total_height;cin >> total_width >> total_height;
Python
for _ in range(int(input())):total_width, total_height = map(int, input().split())x1, y1, x2, y2 = map(int, input().split())w1 = x2 - x1h1 = y2 - y1w2, h2 = map(int, input().split())ans = float("inf")# Calculating minimum and maximum X and Y values needed to fit in their places
Java
import java.io.*;import java.util.*;public class TwoTables {static InputReader r = new InputReader(System.in);static PrintWriter pw = new PrintWriter(System.out);public static void main(String[] args) {int n = r.nextInt();for (int i = 0; i < n; i++) {
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!