Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2022 /finals /ml_modeling.cpp
wjomlex's picture
2022 Problems
f7ba5f2 verified
raw
history blame
2.56 kB
#include <algorithm>
#include <cmath>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
const int SAMPLES = 500;
const int BOUNDS = 50;
int N;
vector<pair<double, double>> P;
inline double distsq(double dx, double dy) { return dx * dx + dy * dy; }
bool check(int x, int y, int r) {
double Rsq = r*r;
for (int i = 0; i < min(N, SAMPLES); i++) {
if (distsq(x - P[i].first, y - P[i].second) > Rsq) {
return false;
}
}
return true;
}
void solve() {
cin >> N;
P.resize(N);
for (int i = 0; i < N; i++) {
cin >> P[i].first >> P[i].second;
}
// Generate valid circles.
vector<tuple<int, int, int>> circles;
for (int Ax = 0; Ax <= BOUNDS; Ax++) {
for (int Ay = 0; Ay <= BOUNDS; Ay++) {
int Ar = 1;
while (Ar <= BOUNDS && !check(Ax, Ay, Ar)) {
Ar++;
}
if (Ar <= BOUNDS) {
circles.push_back({Ax, Ay, Ar});
}
}
}
// Try for all valid circles.
int bestAx = -1, bestAy = -1, bestBx = -1, bestBy = -1, bestR = -1;
double bestArea = 999999;
for (int a = 0; a < (int)circles.size(); a++) {
auto [Ax, Ay, R] = circles[a];
for (int b = a + 1; b < (int)circles.size(); b++) {
auto [Bx, By, Br] = circles[b];
if (R != Br) {
continue;
}
double dsq = distsq(Ax - Bx, Ay - By);
// Fail if circles don't overlap.
if (dsq >= 4 * R * R) {
continue;
}
// Choose the answer that minimizes the area of the overlap.
double d = sqrt(dsq);
double fouradsq = sqrt((4 * R * R) - dsq);
double area = R * R * M_PI;
area -= 2 * R * R * atan2(d, fouradsq);
area -= 0.5 * d * fouradsq;
if (area < bestArea) {
bestArea = area;
bestAx = Ax;
bestAy = Ay;
bestBx = Bx;
bestBy = By;
bestR = R;
}
}
}
// Compute sum of squared distance to points from both A and B.
// If B's sum is less, B is actually A.
// Note: We can afford to use all N points for this, no need to sample.
double Asum = 0, Bsum = 0;
for (auto [x, y] : P) {
Asum += distsq(x - bestAx, y - bestAy);
Bsum += distsq(x - bestBx, y - bestBy);
}
if (Bsum < Asum) {
swap(bestAx, bestBx);
swap(bestAy, bestBy);
}
cout << bestAx << " " << bestAy << " " << bestBx << " " << bestBy << " "
<< bestR << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": ";
solve();
}
return 0;
}