Kattis - Maximum Number of Colinear Points

Author: Benjamin Qi

Table of Contents

ExplanationImplementation

Explanation

Add one to the line passing through each pair of points.

Implementation

Time Complexity: O(N2logN)\mathcal{O}(N^2\log N)

C++

#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, int> get_line(pair<int, int> a, pair<int, int> b) {
pair<int, int> z = {b.first - a.first, b.second - a.second};
swap(z.first, z.second);
z.first *= -1;
int g = __gcd(z.first, z.second);
z.first /= g;

Python

import math
from collections import defaultdict
from typing import Tuple
def get_line(a: Tuple[int, int], b: Tuple[int, int]) -> Tuple[Tuple[int, int], int]:
z = (b[0] - a[0], b[1] - a[1])
z = (-z[1], z[0])
g = math.gcd(z[0], z[1])

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!