M - Hypertransmission
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 in A 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 A are 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
The first line of input contains N -- the number of planets in the Galactic Federation (1 <= N <= 1000). 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.
Output
First output D -- the maximal possible number of destabilizing planets, and then followed on the second line by a 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.
Sample Input
4 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 1
Sample Output
4 1.0000
已知n个星球,每个星球播放有自己的节目类型(0或1),当某个星球接收的节目类型与自己的节目类型不同的大于相同的时候,被定义为间谍星球,问最多可以有多少个间谍星球,要保证星球接收节目的半径尽可能小。
思路:保存每两个星球之间的距离,按距离从小到大的顺序排序,枚举各个距离段,求出相同距离段的数量最多的情况即可,如果后面有一样数量的距离段,则不更新,这就保证了星球接收节目的半径尽可能小,注意开始时初始化每个星球接收的节目类型与自己的节目类型相同的数量为1,因为每个星球都可以接受自己的信号
#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=110;
const int nmax = 1020;
const double esp = 1e-8;
const double PI=3.1415926;
double r;
int ans,n;
struct point
{
int x,y,z,val;
point() {}
point(int _x,int _y,int _z):x(_x),y(_y),z(_z)
{
}
point operator -(const point &b)const
{
return point(x-b.x,y-b.y,z-b.z);
}
} p[nmax];
double len(point p1,point p2){
return sqrt((1.0*p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));
}
int dif[nmax],same[nmax];
struct node{
int a,b;
double d;
}dis[nmax*nmax];
bool cmp(node no1,node no2){
return no1.d<no2.d;
}
void solve()
{
r=0;
int num=0;
int maxl=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
dis[num].a=i;
dis[num].b=j;
dis[num++].d=len(p[i],p[j]);
}
}
sort(dis,dis+num,cmp);
ans=0;
for(int i=0;i<num;i++){
if(p[dis[i].a].val==p[dis[i].b].val){
if(dif[dis[i].a]-same[dis[i].a]==1)
ans--;
if(dif[dis[i].b]-same[dis[i].b]==1)
ans--;
same[dis[i].a]++;
same[dis[i].b]++;
}
if(p[dis[i].a].val!=p[dis[i].b].val){
if(dif[dis[i].a]-same[dis[i].a]==0)
ans++;
if(dif[dis[i].b]-same[dis[i].b]==0)
ans++;
dif[dis[i].a]++;
dif[dis[i].b]++;
}
if(i!=num-1&&fabs(dis[i].d-dis[i+1].d)<esp)continue;
//最后一个或距离发生改变的时候要判断更新最大值
if(ans>maxl){
maxl=ans;
r=dis[i].d;
}
}
printf("%d\n%.4lf\n",maxl,r);
}
int main()
{
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].z,&p[i].val);
same[i]=1;
dis[i]=0;
}
solve();
}
return 0;
}