/
分别对x和y利用前缀和求带权中位数。这类题不是经常遇到,所以记录一下。
#include <bits/stdc++.h>
using namespace std;struct Point
{double x, y, w;
}p[105];
double sum[105];bool cmpx(const Point &a,const Point &b)
{return a.x < b.x;
}
bool cmpy(const Point &a,const Point &b)
{return a.y < b.y;
}int main()
{int t, n;cin >> t;while(t--){scanf("%d", &n);for(int i = 0; i < n; ++i)scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].w);sort(p, p+n, cmpx);sum[0] = p[0].w;for(int i = 1; i < n; ++i)sum[i] = sum[i-1] + p[i].w;double tot = sum[n-1], x, y;for(int i = 0; i < n; ++i){double left = (i ? sum[i-1] : 0);double right = sum[n-1] - sum[i];if(left <= tot/2 && right <= tot/2){x = p[i].x;break;}}sort(p, p+n, cmpy);sum[0] = p[0].w;for(int i = 1; i < n; ++i)sum[i] = sum[i-1] + p[i].w;for(int i = 0; i < n; ++i){double left = (i ? sum[i-1] : 0);double right = sum[n-1] - sum[i];if(left <= tot/2 && right <= tot/2){y = p[i].y;break;}}double ans = 0;for(int i = 0; i < n; ++i)ans += p[i].w * (fabs(p[i].x - x) + fabs(p[i].y - y));printf("%.6f %.6f %.6fn", x, y, ans);}return 0;
}
本文发布于:2024-01-30 15:25:20,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659952220973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |