fixed info box & added to Bronze Rectangle Geo

This commit is contained in:
Michael Cao 2020-06-16 19:09:55 -05:00
parent cc99a4ff20
commit 7ce588fc3c
2 changed files with 78 additions and 5 deletions

View file

@ -1,7 +1,7 @@
---
id: rect-geo
title: "Rectangle Geometry"
author: Darren Yao
author: Darren Yao, Michael Cao
---
"Geometry" problems on USACO Bronze are usually quite simple and limited to intersections and unions of squares or rectangles.
@ -11,8 +11,79 @@ author: Darren Yao
- Some only include two or three squares or rectangles, in which case you can simply draw out cases on paper. This should logically lead to a solution.
- Also, the coordinates typically only go up to $1000$, so a program that performs $\approx 1000^2$ operations (ex. with a nested loop) should pass.
## Problems
## Rectangle Class (Java)
A useful class in `Java` for dealing with rectangle geometry problems is the built-in `Rectangle` class. To create a new rectangle, use the following constructor:
```java
//creates a rectangle with upper-left corner at (x,y) with a specified width and height
Rectangle newRect = new Rectangle(x, y, width, height);
```
The `Rectangle` class supports numerous useful methods.
- `firstRect.intersects(secondRect)` checks if two rectangles intersect.
- `firstRect.union(secondRect)` returns a rectangle representing the union of two rectangles.
- `firstRect.contains(x, y)` checks whether the integer point (x,y) exists in firstRect.
- `firstRect.intersect(secondRect)` returns a rectangle representing the intersection of two rectangles.
This class can often lessen the implementation needed in a lot of bronze problems and codeforces problems.
For example, here is a nice implementation of the problem Blocked Billboard (see below). See the editorial [here](http://www.usaco.org/current/data/sol_billboard_bronze_dec17.html) for more information on the solution.
```java
import java.awt.Rectangle; //needed to use Rectangle class
public class BlockedBillboard{
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
Rectangle firstRect = new Rectangle(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
Rectangle secondRect = new Rectangle(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
Rectangle truck = new Rectangle(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
pw.println(getArea(firstRect) + getArea(secondRect)
- getArea(firstRect.intersect(truck)) - getArea(secondRect.intersect(truck)));
pw.close();
}
public static long getArea(Rectangle r){
return r.getHeight() * r.getWidth()
}
}
```
(someone test code pls)
## Rectangle Class (C++)
Unfortunately, C++ doesn't have a built in rectangle class, so you need to write the functions yourself. Here is the solution to Blocked Billboard written in C++ (thanks, Brian Dean!).
```cpp
struct Rect{
int x1, y1, x2, y2;
int area(Rect r){
return (y2 - y1) * (x2 - x1);
}
};
int intersect(Rect p, Rect q){
int xOverlap = max(0, min(p.x2, q.x2) - max(p.x1, q.x1));
int yOverlap = max(0, min(p.y2, q.y2) - max(p.y1, q.y1));
return xOverlap * yOverlap;
}
int main(){
ifstream cin ("billboard.in");
ofstream cout ("billboard.out");
Rect a, b, t; // billboards a, b, and the truck
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2;
cin >> b.x1 >> b.y1 >> b.x2 >> b.y2;
cin >> t.x1 >> t.y1 >> t.x2 >> t.y2;
cout << area(a) + area(b) - intersect(a, t) - intersect(b, t);
}
```
## Problems
- USACO Bronze
- [Fence Painting](http://usaco.org/index.php?page=viewproblem2&cpid=567)
- 1D geometry!!
@ -22,4 +93,6 @@ author: Darren Yao
- [Blocked Billboard II](http://usaco.org/index.php?page=viewproblem2&cpid=783)
- Also rectangles
- Other
- [CF 587 (Div. 3) C: White Sheet](https://codeforces.com/contest/1216/problem/C)
- [CF 587 (Div. 3) C: White Sheet](https://codeforces.com/contest/1216/problem/C)
- See this code (TODO; codeforces is down) for a nice implementation using the Java Rectangle class.

View file

@ -18,8 +18,8 @@ prerequisites:
## Problems
[[info | Pro Tip]]
| Don't just dive into trying to figure out a DP state and transitions -- make some observations if you don't see any obvious DP solution!
[[info | Pro Tip]]
| Don't just dive into trying to figure out a DP state and transitions -- make some observations if you don't see any obvious DP solution!
* [Subtree](https://atcoder.jp/contests/dp/tasks/dp_v)
* [Independent Set](https://atcoder.jp/contests/dp/tasks/dp_p)