The president of the Galactic Federation has recently decided that all planets of the galaxy must establish hyper-radio centers to broadcast their programs. To ensure the process, the government has signed the contract with well known hyper-radio equipment manufacturer Trojan Horse Ltd. By the terms of this contract the company has to provide N hypertransmitters, one for each planet of the Federation.
It is known that there are two main political movements in the galaxy: industrialism and ecologism. On each planet of the galaxy one of these movements has the majority. It is clear that after establishing the hyper-radio station on the planet, the political programs of the station will support the movement that has the majority on this planet.
All transmitters supplied by Trojan Horse Ltd will have the same range, so hyper-radio programs from each planet will be heard at the distance not exceeding R parsecs from it. Since the company director is actually the agent of the Dark Empire, he wants to choose R in such a way, that it would destabilize the political situation in the Galactic Federation.
More precisely, for each planet A let N+(A) be the number of planets where the same political movement as inA has the majority and hyper-radio programs from A are received, including A itself. Similarly, let N-(A) be the number of planets where the other political movement has the majority and hyper-radio programs from Aare received. The planet A is called destabilizing if N+(A) < N-(A).
Your task is to choose such R that the number D of destabilizing planets is maximal possible. Since increasing transmitter's range requires more resources for its manufacturing, you must find the smallest possible R maximizing D.
Input consists of several datasets. The first line of each dataset contains N - the number of planets in the Galactic Federation (1N1000). Next N lines contain four integer numbers xi, yi, zi, and pi each and describe the planets: xi, yi, and zi specify the coordinates of the planet in space, pi = 0 if the industrialists have the majority on the planet and pi = 1 if the ecologists have the majority. All coordinates do not exceed 10 000 by their absolute value. No two planets occupy the same point.
First output D - the maximal possible number of destabilizing planets. After that output non-negative real number R - the minimal range that hyper-radio transmitters must have so that the number of destabilizing planets is D. R must be accurate within 10-4 of the correct answer.
4 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 1
4 1.0000
题意:有n个星球,坐标x,y,z,播放着1或0类节目,现在求一个半径,表示星球的接受信号半径,如果星球接受不同类型节目多于同类型节目,则该星球为间谍星球,现在求让最多星球变成间谍星球且半径要尽可能小。求个数和半径。
思路:先把所有星球两两距离保存下来,然后按距离从小到大,然后去扫描,扫描过程中维护个数最大值和半径最小值即可,求出的半径肯定为某两个星球对的距离。注意几个点:星球自己本身的节目也算能接收,还有距离相同的星球要注意特殊考虑,计算完的才去维护值。
代码:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; const int N = 1005; int n; struct Point { double x, y, z; int p, same, diff; } p[N]; struct D { int a, b; double dis; }d[N * N]; bool cmp(D a, D b) { return a.dis < b.dis; } double cal_dis(Point a, Point b) { return sqrt((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 dn = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { d[dn].a = i; d[dn].b = j; d[dn++].dis = cal_dis(p[i], p[j]); } sort(d, d + dn, cmp); int ansn = 0, diff = 0; double ansp = 0; for (int k = 0; k < dn; k++) { int a = d[k].a, b = d[k].b; if (p[a].p == p[b].p) { if (p[a].diff - p[a].same == 1) diff--; if (p[b].diff - p[b].same == 1) diff--; p[a].same++; p[b].same++; } else { if (p[a].same == p[a].diff) diff++; if (p[b].same == p[b].diff) diff++; p[a].diff++; p[b].diff++; } if (k != dn - 1 && fabs(d[k].dis - d[k + 1].dis) < 1e-9) continue; if (diff > ansn) { ansn = diff; ansp = d[k].dis; } } printf("%d\n%.4lf\n", ansn, ansp); } int main() { while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%d", &p[i].x, &p[i].y, &p[i].z, &p[i].p); p[i].same = 1; p[i].diff = 0; } solve(); } return 0; }