QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#261294#7336. FactorygondozuTL 0ms0kbC++201.9kb2023-11-22 19:57:262023-11-22 19:57:26

Judging History

你现在查看的是最新测评结果

  • [2023-11-22 19:57:26]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-11-22 19:57:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double MAX = 1e6, EPS = 1e-8;

struct Point{
    double x, y;
};

double getDistance(double x, double y, const vector<Point>& points){
    double dis = 0;
    for(auto p : points){
        dis += sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));
    }
    return dis;
}

// returns the minimum distance, and the y coordinate of the optimal point.
pair<double, double> ternarySearchY(double x, const vector<Point>& points){
    double l = -MAX, r = MAX;
    while (l <= r){
        double m1 = l + (r-l)/3;
        double m2 = r - (r-l)/3;

        double dis1 = getDistance(x, m1, points),
               dis2 = getDistance(x, m2, points);

        if(dis1 < dis2)
            r = m2;
        else
            l = m1;
    }
    return {getDistance(x, l, points), l};
}

double ternarySearchX(const vector<Point>& points){
    double l = -MAX, r = MAX;
    while (l <= r){
        double  m1 = l + (r-l)/3,
                m2 = r - (r-l)/3;

        double dis1,y1,dis2,y2;
        tie(dis1, y1) = ternarySearchY(m1, points);
        tie(dis2, y2) = ternarySearchY(m2, points);

        if(dis1 < dis2)
            r = m2;
        else
            l = m1;
    }
    return l;
}

// gets the point that minimizes the total distance from all given points
Point getCentralPoint(const vector<Point>& points){
    double x = ternarySearchX(points);
    return {x, ternarySearchY(x, points).second};
}

void solveTC(){
    int n;
    cin >> n;
    int x, y;
    vector<Point> points;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        points.push_back({(double)x, (double)y});
    }
    Point ans = getCentralPoint(points);
    cout << fixed << setprecision(7) << ans.x << ' ' << ans.y << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--){
        solveTC();
    }
    return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

1
3
-3 0
0 3
3 0

output:


result: