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
More precisely, for each planet
Your task is to choose such
Input
Input consists of several datasets. The first line of each dataset contains
Output
First output
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个星球上安装广播设备, 在这群星球中有2种体制分别是工业主义和生态主义, 所有的设备
传播距离不超过R, 如果某个星球可以收到相同体制的广播数量 < 不同体制的广播数量, 那么这个
星球就称为不稳定, 现在要你计算出在选择出一个R, 并且使得不稳定的星球数量尽可能多, R尽量小.
解题思路:
1. 想一想R的上下界, 肯定是两两星球的距离最大和最小值, 从小到大扫描两个星球的距离,
同时不断更新每条边的两个星球的不同类型广播数量.
代码: #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAX 1005
struct Node
{
int type;
int state[2];
}node[MAX];
struct Edge
{
int p1, p2;
double dist;
bool operator <(const Edge &x) const
{
return dist < x.dist;
}
}edges[MAX*MAX];
int n;
int x[MAX], y[MAX], z[MAX];
int num, cur, ans;
int ansDist;
inline double getDist(int i, int j)
{
return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]);
}
void update(int index)
{
int i = edges[index].p1, j = edges[index].p2;
cur -= node[i].state[ node[i].type ] < node[i].state[ 1^node[i].type ];
cur -= node[j].state[ node[j].type ] < node[j].state[ 1^node[j].type ];
++node[i].state[ node[j].type ];
++node[j].state[ node[i].type ];
cur += node[i].state[ node[i].type ] < node[i].state[ 1^node[i].type ];
cur += node[j].state[ node[j].type ] < node[j].state[ 1^node[j].type ];
}
int main()
{
//freopen("input.txt", "r", stdin);
int i, j;
while(scanf("%d", &n) != EOF)
{
for(i = 0; i < n; ++i)
{
scanf("%d %d %d %d", &x[i], &y[i], &z[i], &node[i].type);
node[i].state[ node[i].type ] = 1;
node[i].state[ 1^node[i].type ] = 0;
}
num = 0;
for(i = 0; i < n; ++i)
{
for(j = i+1; j < n; ++j)
{
edges[++num].p1 = i;
edges[num].p2 = j;
edges[num].dist = getDist(i, j);
}
}
sort(edges+1, edges+num+1);
edges[num+1].dist = -1;
ans = cur = 0;
ansDist = 0;
for(i = 1; i <= num; ++i)
{
int left = i;
int right = i;
update(left);
while(edges[right+1].dist == edges[left].dist)
{
right++;
update(right);
}
if(cur > ans)
{
ans = cur;
ansDist = edges[left].dist;
}
i = right;
}
printf("%d %.4lf\n", ans, sqrt(ansDist));
}
return 0;
}