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