题意:n个星球上都有一个广播,广播范围是r(和它范围不超过r都可听到广播),广播种类有A和B,如果一个星球可以听到的广播和自身广播不一样的有a个星球,一样的有b个星球,a > b说明这个星球是不稳定的,问给出一个r使不稳定星球尽量多,然后让r尽量少。
题解:先把所有星球之间距离计算出来,然后根据距离排序,把所有距离相同的边放到一起计算不稳定星球的数量,找到最大数量星球,然后再更新r。
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 1005;
int n, val[N];
struct Point {
int x, y, z;
int flag;
}p[N];
struct Edge {
int s, t;
int dis;
}edge[N * N];
bool cmp(Edge a, Edge b) {
return a.dis < b.dis;
}
int count(Point a, Point b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z);
}
void solve() {
int cnt = 0;
for (int i = 0; i < n; i++) {
val[i] = 1;
for (int j = i + 1; j < n; j++) {
edge[cnt].s = i;
edge[cnt].t = j;
edge[cnt++].dis = count(p[i], p[j]);
}
}
sort(edge, edge + cnt, cmp);
int temp = 0, i = 0, res = 0, r = 0, j;
while (i < cnt) {
for (j = i; j < cnt && edge[j].dis == edge[i].dis; j++) {
if (p[edge[j].s].flag != p[edge[j].t].flag) {
if (--val[edge[j].s] == -1)
temp++;
if (--val[edge[j].t] == -1)
temp++;
}
else {
if (++val[edge[j].s] == 0)
temp--;
if (++val[edge[j].t] == 0)
temp--;
}
}
if (temp > res) {
res = temp;
r = edge[i].dis;
}
i = j;
}
printf("%d\n%.4lf\n", res, sqrt(r * 1.0));
}
int main() {
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; i++)
scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i].z, &p[i].flag);
solve();
}
return 0;
}