LA2963
来源:互联网 发布:painter for mac 中文 编辑:程序博客网 时间:2024/05/21 07:03
题意: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 0
- LA2963
- 今日失误
- TINY编译器《编译原理》
- 《leetCode》:Maximum Product Subarray
- KVM原理与架构系列之三 CPU虚拟化篇
- 18. 4Sum LeetCode
- LA2963
- DataSet和List<T> 泛型之间互相转换
- I-Jetty部署war包到安卓手机
- Paper Reading 3:Continuous control with Deep Reinforcement Learning
- java多线程中this与Thread.currentThread()返回值的引用问题
- QImage与QPixmap完全解析 转:http://blog.csdn.net/jiong_1988/article/details/7472159
- 人为什么会生病
- 数据窗口key Modification与sqlserver触发器for update 配合
- 大话设计模式—单例模式