Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("O3,fast-math")
- #include <algorithm>
- #include <array>
- #include <cassert>
- #include <chrono>
- #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 sqrt(x * x + y * y); }
- ld Len2() const { return x * x + y * 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; }
- bool operator==(const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; }
- bool operator!=(const Point& p1, const Point& p2) { return !operator==(p1, p2); }
- bool operator==(const Vector& v1, const Vector& v2) { return v1.x == v2.x && v1.y == v2.y; }
- bool operator!=(const Vector& v1, const Vector& v2) { return !operator==(v1, v2); }
- istream& operator>>(istream& in, Point& v) { in >> v.x >> v.y; return in; }
- istream& operator>>(istream& in, Vector& v) { in >> v.x >> v.y; return in; }
- ostream& operator<<(ostream& out, const Point& v) { out << v.x << " " << v.y; return out; }
- ostream& operator<<(ostream& out, const Vector& v) { out << v.x << " " << v.y; return out; }
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- vector<Point> GeneratePoints(int n) {
- vector<Point> points(n);
- uniform_real_distribution<ld> dist(0, 1000.);
- for (int i = 0; i < n; ++i) {
- points[i] = {dist(rng), dist(rng)};
- }
- return points;
- }
- ld Diametr(const vector<Point>& points) {
- ld max_dist = 0;
- for (int i = 0; i < points.size(); ++i) {
- for (int j = i + 1; j < points.size(); ++j) {
- max_dist = max(max_dist, (points[i] - points[j]).Len());
- }
- }
- return max_dist;
- }
- ld DiametrOpt(const vector<Point>& points) {
- ld max_dist = 0;
- for (int i = 0; i < points.size(); ++i) {
- for (int j = i + 1; j < points.size(); ++j) {
- max_dist = max(max_dist, (points[i] - points[j]).Len2());
- }
- }
- return sqrt(max_dist);
- }
- void Solve() {
- int n; cin >> n;
- auto points = GeneratePoints(n);
- auto start = chrono::high_resolution_clock::now().time_since_epoch().count();
- ld res1 = Diametr(points);
- auto end = chrono::high_resolution_clock::now().time_since_epoch().count();
- auto start2 = end;
- ld res2 = DiametrOpt(points);
- auto end2 = chrono::high_resolution_clock::now().time_since_epoch().count();
- cout << "Diametr: " << res1 << "\n";
- cout << "DiametrOpt: " << res2 << "\n";
- cout << "Time: " << (end - start) / 1'000'000 << "ms\n";
- cout << "TimeOpt: " << (end2 - start2) / 1'000'000 << "ms\n";
- cout << "Profit: " << (1. - (ld)(end2 - start2) / (end - start)) * 100. << "%\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
Advertisement