smatskevich

Seminar3

Jan 29th, 2026
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <cassert>
  4. #include <climits>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <limits>
  10. #include <list>
  11. #include <map>
  12. #include <numeric>
  13. #include <queue>
  14. #include <random>
  15. #include <set>
  16. #include <sstream>
  17. #include <stack>
  18. #include <string>
  19. #include <tuple>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <vector>
  23.  
  24. #define all(x) x.begin(), x.end()
  25. using namespace std;
  26. using ll = long long;
  27. using ld = long double;
  28. const double eps = 1e-10;
  29. const int mod = 1000000007;
  30.  
  31. const ld PI = atan2l(0., -1.);
  32.  
  33. struct Point { ld x; ld y; };
  34. struct Vector {
  35.   ld x; ld y;
  36.   ld Len() const { return sqrtl(x * x + y * y); }
  37.   ld Len2() const { return x * x + y * y; }
  38. };
  39. struct Line {
  40.   ld a; ld b; ld c;
  41.   Line(const Point& p1, const Point& p2) { a = p1.y - p2.y; b = p2.x - p1.x; c = -a * p1.x - b * p1.y; }
  42.   Line(const Point& p, const Vector& v) { a = v.y; b = -v.x; c = -a * p.x - b * p.y; }
  43. };
  44.  
  45. bool operator==(const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; }
  46. Point operator-(const Point& p) { return {-p.x, -p.y}; }
  47. Point operator+(const Point& p, const Vector& v) { return {p.x + v.x, p.y + v.y}; }
  48. Point operator+(const Vector& v, const Point& p) { return {p.x + v.x, p.y + v.y}; }
  49. Point operator-(const Point& p, const Vector& v) { return {p.x - v.x, p.y - v.y}; }
  50. Vector operator-(const Point& p1, const Point& p2) { return {p1.x - p2.x, p1.y - p2.y}; }
  51. Vector operator+(const Vector& v1, const Vector& v2) { return {v1.x + v2.x, v1.y + v2.y};}
  52. Vector operator-(const Vector& v1, const Vector& v2) { return {v1.x - v2.x, v1.y - v2.y}; }
  53. ld DotProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.x + v1.y * v2.y; }
  54. ld CrossProduct(const Vector& v1, const Vector& v2) { return v1.x * v2.y - v1.y * v2.x; }
  55. istream& operator>>(istream& in, Point& v) { return in >> v.x >> v.y; }
  56. istream& operator>>(istream& in, Vector& v) { return in >> v.x >> v.y; }
  57. ostream& operator<<(ostream& out, const Point& v) { return out << v.x << " " << v.y; }
  58. ostream& operator<<(ostream& out, const Vector& v) { return out << v.x << " " << v.y; }
  59. bool operator<(const Point& p1, const Point& p2) { return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); }
  60. namespace std {
  61.   template <> struct hash<Point> {
  62.     size_t operator()(const Point& p) const { return hash<ll>()(p.x) ^ hash<ll>()(p.y); }
  63.   };
  64. }
  65. Point Center(const Point& p1, const Point& p2) { return {(p1.x + p2.x) / 2., (p1.y + p2.y) / 2.}; }
  66. Point Intersection(const Line& l1, const Line& l2) {
  67.   ld d = l1.a * l2.b - l1.b * l2.a;
  68.   if (fabsl(d) < eps) return {INFINITY, INFINITY};
  69.   return {(l2.c * l1.b - l1.c * l2.b) / d, (l1.c * l2.a - l2.c * l1.a) / d};
  70. }
  71.  
  72. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  73.  
  74. vector<Point> GeneratePoints(int n) {
  75.   uniform_real_distribution<ld> dist(-1000., 1000.);
  76.   vector<Point> p(n);
  77.   for (auto& x : p) x = {dist(rng), dist(rng)};
  78.   return p;
  79. }
  80.  
  81. ld Diameter(const vector<Point>& p) {
  82.   ld d = 0.;
  83.   for (int i = 0; i < p.size(); ++i) {
  84.     for (int j = i + 1; j < p.size(); ++j) {
  85.       d = max(d, (p[i] - p[j]).Len());
  86.     }
  87.   }
  88.   return d;
  89. }
  90.  
  91. ld DiameterOpt(const vector<Point>& p) {
  92.   ld d = 0.;
  93.   for (int i = 0; i < p.size(); ++i) {
  94.     for (int j = i + 1; j < p.size(); ++j) {
  95.       d = max(d, (p[i] - p[j]).Len2());
  96.     }
  97.   }
  98.   return sqrt(d);
  99. }
  100.  
  101. void Solve() {
  102.   int n; cin >> n;
  103.   vector<Point> p = GeneratePoints(n);
  104.   auto start = chrono::high_resolution_clock::now();
  105.   ld d1 = Diameter(p);
  106.   auto medium = chrono::high_resolution_clock::now();
  107.   ld d2 = DiameterOpt(p);
  108.   auto end = chrono::high_resolution_clock::now();
  109.   cout << "Diameter: " << d1 << ", " << d2 << endl;
  110.   auto duration1 = medium - start;
  111.   auto duration2 = end - medium;
  112.   cout << "Time1: " << chrono::duration_cast<chrono::microseconds>(duration1).count() << "mcs\n";
  113.   cout << "Time2: " << chrono::duration_cast<chrono::microseconds>(duration2).count() << "mcs\n";
  114. }
  115. int main() {
  116.   ios::sync_with_stdio(false);
  117.   cin.tie(nullptr);
  118.   cout.tie(nullptr);
  119.   Solve();
  120.   return 0;
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment