高效算法设计专项:LA 2963
来源:互联网 发布:mac os x 卸载 编辑:程序博客网 时间:2024/05/21 14:01
刚开始完全没思路,看了训练指南上的提示说是扫描法,然后往这方面想。仔细分析题目发现可以用O(n^2)的时间解决。不难发现可能的结果只可能是n个点两两距离的,然后可以从小到大扫描这n*(n-1)/2条边,然后维护当前符合条件的planet的个数。需要注意处理边长相等的情况。
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;#define M 1010const double eps=1e-6;int dcmp(double x){ if(fabs(x)<eps) return 0; else return x<0?-1:1;}struct point{ int x,y,z,p;} a[M];struct edge{ int u,v; double l; edge(int u=0,int v=0,double l=0):u(u),v(v),l(l){} bool operator<(const edge& tmp) const {return dcmp(l-tmp.l)<0;}} e[M*M];double length(int i,int j){ int x=a[i].x-a[j].x,y=a[i].y-a[j].y,z=a[i].z-a[j].z; return sqrt(x*x+y*y+z*z);}int n,m;int c[M];int main(){ freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].z,&a[i].p); c[i]=1; } m=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { e[m++]=edge(i,j,length(i,j)); } } sort(e,e+m); int ans=0,cur=0; double len=0; for(int i=0;i<m;i++) { int u=e[i].u,v=e[i].v; double l=e[i].l; if(a[u].p!=a[v].p) { c[u]--; c[v]--; if(c[u]==-1) cur++; if(c[v]==-1) cur++; } else { c[u]++; c[v]++; if(c[u]==0) cur--; if(c[v]==0) cur--; } if(i!=m-1&&dcmp(l-e[i+1].l)==0) continue; if(cur>ans) { ans=cur; len=l; } } printf("%d\n",ans); printf("%.4f\n",len); } return 0;}
- 高效算法设计专项:LA 2963
- 高效算法设计专项:LA 2689
- 高效算法设计专项:LA 4726
- 高效算法设计专项:LA 4356
- 高效算法设计专项:UVa 10125
- 高效算法设计专项:UVa 10391
- 高效算法设计专项:UVa 10810
- 高效算法设计专项:UVa 10730
- 高效算法设计专项:UVa 10535
- 高效算法设计专项:UVa 11054
- 高效算法设计专项:UVa 11572
- 高效算法设计专项:UVa 10827
- 高效算法设计专项:UVa 10691
- SEERC 2006 Subsequence, 高效算法设计 ,LA 2678
- 搜索专项:LA 5844
- 字符串专项:LA 5913
- 字符串专项:LA 2755
- 字符串专项:LA 4126
- More Effective C++
- spring 获取 datasource
- 网站性能指南
- Java中字符串比较的注意点
- Corosync+Pacemaker实现简单的Web HA集群
- 高效算法设计专项:LA 2963
- MFC下MD5加密算法实现,带key(32位MD5小写)
- 堆排序 一个综合了插入排序和二路归并特点的排序算法(未测试)
- LoadRunner结果分析 – TPS
- Object-C 实现判断一个字符串是否是数字
- 动态规划专项intermediate:UVa 11400
- LoadRunner压力测试结果分析探讨
- 性能问题定位及调优
- 杂题专项:UVa 818