Table of Contents
Edit on GithubCF - Cut Length
Author: Benjamin Qi
My Code (without template):
1#include <bits/stdc++.h>2using namespace std;34using ll = long long;5using ld = long double;6using db = double;7using str = string; // yay python!89using pi = pair<int,int>;10using pl = pair<ll,ll>;
Time Complexity:
We'll process each of the lines in time each. For a fixed line, let and be two points on the line. Suppose for simplicity that the line is parallel to the -axis.
First, if no vertex of the polygon lies on the line then we can compute all the intersection points of the sides with the line and add / subtract their -coordinates appropriately to get the answer. In my solution, dot(b-a,p)/abs(b-a)
essentially computes the -coordinate of some point .
To deal with vertices of the polygon that lie on the line, we can envision shifting the line up or down by some small amount so that no vertex lies on the line. This incorrectly leaves out some sides of the polygon that are collinear with the original line, so we should add the lengths of these sides to the answer. These sides are dealt with by the second if
statement within the loop.
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!