LA2963

题意: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();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值