Hướng dẫn giải của Phủ sóng


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt


Code mẫu của happyboy99x

#include<bits/stdc++.h>
using namespace std;

typedef complex<double> Point;

const int N = 200, MAX = 1e5;
Point p[N];
int n;

void enter() {
    cin >> n;
    for(int i = 0; i < n; ++i)
        cin >> real(p[i]) >> imag(p[i]);
}

double minRadius(const Point &center) {
    double res = 0;
    for(int i = 0; i < n; ++i)
        res = max(res, abs(center - p[i]));
    return res;
}

double f(double x) {
    double low = -MAX, high = MAX;

    for(int t = 0; t < 75; ++t) {
        double m1 = (2 * low + high) / 3, m2 = (low + 2 * high) / 3;
        if(minRadius(Point(x, m1)) < minRadius(Point(x, m2))) high = m2;
        else low = m1;
    }

    return minRadius(Point(x, (low + high) / 2));
}

void solve() {
    double low = -MAX, high = MAX;

    for(int t = 0; t < 75; ++t) {
        double m1 = (2 * low + high) / 3, m2 = (low + 2 * high) / 3;
        if(f(m1) < f(m2)) high = m2;
        else low = m1;
    }

    cout << fixed << setprecision(6) << f((low + high) / 2) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    enter();
    solve();
    return 0;
}

Code mẫu của ladpro98

#include <bits/stdc++.h>
#define X first
#define Y second
#define sqr(a) ((a) * (a))

const int N = 200;
const int INF = 1e9;
const int LIMIT = 60;

using namespace std;

typedef pair<double, double> point;

int n;
point a[N];

double dist(const point &a, const point &b) {
    return sqr(a.X - b.X) + sqr(a.Y - b.Y);
}

double evaluate(point C) {
    double ans = 0;
    for (int i = 0; i < n; ++i)
        ans = max(ans, dist(C, a[i]));
    return ans;
}

double ternarySearchY(double x) {
    double ans = INF;
    double l = -1e4, r = 1e4;
    for (int loop = 0; loop < LIMIT; ++loop) {
        double tl = l + (r - l) / 3;
        double tr = r - (r - l) / 3;
        double vl = evaluate(point(x, tl));
        double vr = evaluate(point(x, tr));
        ans = min(ans, min(vl, vr));
        if (vl < vr)
            r = tr;
        else
            l = tl;
    }
    return ans;
}

double ternarySearchX() {
    double ans = INF;
    double l = -1e4, r = 1e4;
    for (int loop = 0; loop < LIMIT; ++loop) {
        double tl = l + (r - l) / 3;
        double tr = r - (r - l) / 3;
        double vl = ternarySearchY(tl);
        double vr = ternarySearchY(tr);
        ans = min(ans, min(vl, vr));
        if (vl < vr)
            r = tr;
        else
            l = tl;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i].X >> a[i].Y;
    double ans = ternarySearchX();
    cout << setprecision(6) << fixed << sqrt(ans) << endl;
    return 0;
}

Code mẫu của RR

//Written by RR

{$MODE OBJFPC}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
  MAXN          =       100111;
  eps           =       1e-9;

type
  point         =       record x,y:double; end;
  circle        =       record x,y,r:double; end;

var
  f1,f2         :       text;
  test,n        :       longint;
  a             :       array[1..MAXN] of point;
  res           :       circle;

procedure openF;
    begin
      assign(f1,FINP); reset(f1);
      assign(f2,FOUT); rewrite(f2);
    end;

procedure closeF;
    begin
      close(f1);
      close(f2);
    end;

procedure inp;
    var
      i:longint;
    begin
      read(f1,n);
      for i:=1 to n do
        with a[i] do
          read(f1,x,y);
    end;

procedure update2(a,b:point);
    begin
      res.x:=(a.x+b.x)/2;
      res.y:=(a.y+b.y)/2;
      res.r:=sqr(a.x-res.x) + sqr(a.y-res.y);
    end;

procedure update3(a,b,c:point);
    var
      s,u,v:double;
    begin
      u:=(c.y-b.y)/2-(c.x-b.x)*(a.x-b.x)/2/(b.y-a.y);
      v:=(c.y-a.y)*(a.x-b.x)/(b.y-a.y)-(a.x-c.x);
      s:=u/v;
      res.x:=(a.x+c.x)/2+s*(c.y-a.y);
      res.y:=(a.y+c.y)/2+s*(a.x-c.x);
      res.r:=sqr(a.x-res.x)+sqr(a.y-res.y);
    end;

function isInside(a:point):boolean;
    begin
      exit( sqr(a.x-res.x) + sqr(a.y-res.y) < res.r+eps );
    end;

procedure solve;
    var
      i,j,k:longint;
    begin
      update2(a[1],a[2]);
      for i:=3 to n do
        if not isInside(a[i]) then
          begin
            update2(a[1],a[i]);
            for j:=1 to i-1 do
              if not isInside(a[j]) then
                begin
                  update2(a[i],a[j]);
                  for k:=1 to j-1 do
                    if not isInside(a[k]) then
                      update3(a[i],a[j],a[k]);
                end;
          end;
      writeln(f2,sqrt(res.r):0:6);
//      writeln(f2,res.x:0:2,' ',res.y:0:2);
    end;

begin
  openF;
  test := 1;
  for test:=1 to test do
    begin
      inp;
      if (n = 1) then writeln('0.000000')
      else solve;
    end;
  closeF;
end.

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef long double ld;
//typedef double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(int i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(int i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define nl puts("")
#define sp printf(" ")
#define ok puts("ok")
//#include <conio.h>

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << " "; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> struct Triple { T x, y, z; Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} };
template<class T> Triple<T> euclid(T a, T b) { if (b == 0) return Triple<T>(1, 0, a); Triple<T> r = euclid(b, a % b); return Triple<T>(r.y, r.x - a / b * r.y, r.z); }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }
const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0;
char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; }
void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); }
int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; }
template<class T> bool gi(T &v) {
    v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc();
    while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true;
}

const double PI = 2 * acos(0.0);
const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dr[] = {0, 0, -1, +1};
const int dc[] = {-1, +1, 0, 0};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ld eps = ld(1e-13);
const ll mod = 1000000007;
typedef pair<int, int> II;

#define maxn 1005

struct Point{
    ld x, y;
    Point(){};
    Point(ld _x, ld _y){
        x = _x; y = _y;
    }
}A[maxn];

int n;

ld cal(ld x, ld y){
    ld res = 0;
    Rep(i, n){
        res = max(res, sqrt(sqr(A[i].x - x) + sqr(A[i].y - y)));
    }
    return res;
}

ld f(ld x){
    ld u = -10000, v = 10000, r1, r2;
    Rep(run, 100){
        r1 = (u + u + v) / 3;
        r2 = (u + v + v) / 3;
        if(cal(x, r1) < cal(x, r2)) v = r2;
        else u = r1;
    }

    return cal(x, u);
}

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//    cout << "x" << endl;

    cin >> n;
    Rep(i, n){
        cin >> A[i].x >> A[i].y;
    }

    ld u = -10000, v = 10000, r1, r2;
    Rep(run, 100){
        r1 = (u + u + v) / 3;
        r2 = (u + v + v) / 3;
        if(f(r1) < f(r2)){
            v = r2;
        }
        else u = r1;
    }

    cout << fixed << setprecision(6);
    cout << f(u) << endl;


    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.