Advertisement
smatskevich

Seminar3

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