Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <array>
- #include <cassert>
- #include <climits>
- #include <cmath>
- #include <cstring>
- #include <iomanip>
- #include <iostream>
- #include <limits>
- #include <list>
- #include <map>
- #include <numeric>
- #include <queue>
- #include <random>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <string>
- #include <tuple>
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- #define all(x) x.begin(), x.end()
- using namespace std;
- using ll = long long;
- using ld = long double;
- const double eps = 1e-10;
- const int mod = 1000000007;
- const ld PI = atan2l(0., -1.);
- struct Point { ld x; ld y; };
- struct Vector {
- ld x; ld y;
- ld Len() const { return sqrtl(x * x + y * y); }
- ld Len2() const { return x * x + y * y; }
- };
- struct Line {
- ld a; ld b; ld c;
- Line(const Point& p1, const Point& p2) { a = p1.y - p2.y; b = p2.x - p1.x; c = -a * p1.x - b * p1.y; }
- Line(const Point& p, const Vector& v) { a = v.y; b = -v.x; c = -a * p.x - b * p.y; }
- };
- bool operator==(const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; }
- Point operator-(const Point& p) { return {-p.x, -p.y}; }
- Point operator+(const Point& p, const Vector& v) { return {p.x + v.x, p.y + v.y}; }
- Point operator+(const Vector& v, const Point& p) { return {p.x + v.x, p.y + v.y}; }
- Point operator-(const Point& p, const Vector& v) { return {p.x - v.x, p.y - v.y}; }
- Vector operator-(const Point& p1, const Point& p2) { return {p1.x - p2.x, p1.y - p2.y}; }
- Vector operator+(const Vector& v1, const Vector& v2) { return {v1.x + v2.x, v1.y + v2.y};}
- Vector operator-(const Vector& v1, const Vector& v2) { return {v1.x - v2.x, v1.y - v2.y}; }
- ld DotProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.x + v1.y * v2.y; }
- ld CrossProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.y - v1.y * v2.x; }
- istream& operator>>(istream& in, Point& v) { return in >> v.x >> v.y; }
- istream& operator>>(istream& in, Vector& v) { return in >> v.x >> v.y; }
- ostream& operator<<(ostream& out, const Point& v) { return out << v.x << " " << v.y; }
- ostream& operator<<(ostream& out, const Vector& v) { return out << v.x << " " << v.y; }
- bool operator<(const Point& p1, const Point& p2) { return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); }
- namespace std {
- template <> struct hash<Point> {
- size_t operator()(const Point& p) const { return hash<ll>()(p.x) ^ hash<ll>()(p.y); }
- };
- }
- Point Center(const Point& p1, const Point& p2) { return {(p1.x + p2.x) / 2., (p1.y + p2.y) / 2.}; }
- Point Intersection(const Line& l1, const Line& l2) {
- ld d = l1.a * l2.b - l1.b * l2.a;
- if (fabsl(d) < eps) return {INFINITY, INFINITY};
- return {(l2.c * l1.b - l1.c * l2.b) / d, (l1.c * l2.a - l2.c * l1.a) / d};
- }
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- vector<Point> GeneratePoints(int n) {
- uniform_real_distribution<ld> dist(-1000., 1000.);
- vector<Point> p(n);
- for (auto& x : p) x = {dist(rng), dist(rng)};
- return p;
- }
- ld Diameter(const vector<Point>& p) {
- ld d = 0.;
- for (int i = 0; i < p.size(); ++i) {
- for (int j = i + 1; j < p.size(); ++j) {
- d = max(d, (p[i] - p[j]).Len());
- }
- }
- return d;
- }
- ld DiameterOpt(const vector<Point>& p) {
- ld d = 0.;
- for (int i = 0; i < p.size(); ++i) {
- for (int j = i + 1; j < p.size(); ++j) {
- d = max(d, (p[i] - p[j]).Len2());
- }
- }
- return sqrt(d);
- }
- void Solve() {
- int n; cin >> n;
- vector<Point> p = GeneratePoints(n);
- auto start = chrono::high_resolution_clock::now();
- ld d1 = Diameter(p);
- auto medium = chrono::high_resolution_clock::now();
- ld d2 = DiameterOpt(p);
- auto end = chrono::high_resolution_clock::now();
- cout << "Diameter: " << d1 << ", " << d2 << endl;
- auto duration1 = medium - start;
- auto duration2 = end - medium;
- cout << "Time1: " << chrono::duration_cast<chrono::microseconds>(duration1).count() << "mcs\n";
- cout << "Time2: " << chrono::duration_cast<chrono::microseconds>(duration2).count() << "mcs\n";
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment