ACM: uva 1325 -&n…

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+(Abe 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-(Abe 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 

Input consists of several datasets. The first line of each dataset contains N - the number of planets in the Galactic Federation (1 N 1000). Next N lines contain four integer numbers xiyizi, and pi each and describe the planets: xiyi, and zi specify the coordinates of the planet in space, pi = 0if 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. 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 DR 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个星球上安装广播设备, 在这群星球中有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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值