HDU--4007[Dave] O(N^2)枚举
来源:互联网 发布:知乎 金融炼金术 编辑:程序博客网 时间:2024/05/16 12:07
题目意思:
给你N个点和一个边长为R的正方形,为你用这个正方形最多可以覆盖几个点(在正方形边界上的点也算)。
注意:正方形的边一定平行于坐标轴。
思路:
对N个点的y坐标进行排序,然后O(N)枚举正方形的下边界。取出N个点中y坐标在上下边界范围内的点。然后O(N)复杂度求出长度为R的边最多可以覆盖的点。所以整体枚举的复杂度为O(N^2)。
CODE:
/*O(N^2)枚举*/#include <iostream>#include <cstdio>#include <algorithm>#include <memory.h>#define MAXN 1005#define INF 2e9#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)using namespace std;struct Node{ int x,y;}node[MAXN];int xx[MAXN],yy[MAXN];int xcnt,ycnt;int N,R;void Init(){ int i; for(i=1;i<=N;i++) { scanf("%d%d",&node[i].x,&node[i].y); yy[i]=node[i].y; } sort(yy+1,yy+1+N);}void Solve(){ int i,j,e,ans=0; for(i=1;i<=N;i++) { xcnt=0; for(j=1;j<=N;j++) { if(node[j].y>=yy[i]&&node[j].y<=yy[i]+R) xx[xcnt++]=node[j].x; } sort(xx,xx+xcnt); xx[xcnt++]=INF; e=0; for(j=0;j<xcnt-1;j++) { while(xx[e]<=xx[j]+R) e++; ans=max(ans,(e-j)); } } printf("%d\n",ans);}int main(){ while(scanf("%d%d",&N,&R)!=EOF) { Init(); Solve(); }return 0;}
- HDU--4007[Dave] O(N^2)枚举
- hdu 4007 枚举 Dave
- HDU 4007 Dave 枚举
- hdu 4007 暴力枚举 Dave
- HDU 4007:Dave (枚举)
- HDU 4007 Dave (暴力枚举)
- hdu 1510-O(n^3)枚举
- hdu 4007 Dave
- hdu 4007 Dave
- hdu 4007 Dave
- HDU 4007 Dave
- hdu 4007 Dave
- HDU 4007 Dave
- HDU 4007 Dave
- hdu 4961 枚举约数O(n*sqrt(n))
- HDU 4007 Dave 线段树
- HDU--3721[Building Roads] 枚举+求最长路O(N^2)
- HDU--3884[Hinanai Tenshi’s peach garden] 枚举汇聚点O(N^2)
- android关于应用程序退出后Service重启
- C++学习笔记
- Android入门之TextView(纯java)
- 【转载】poj DP题
- javascript工具类 (判断闰年,汉字,IP,精度等。。)
- HDU--4007[Dave] O(N^2)枚举
- 【转载】HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- Oracle日期函数与应用(转)
- Android应用程序进程启动过程的源代码分析
- 笔试题目汇总
- MM学习总结
- 软件生命周期模型
- 软件生存期模型介绍
- How to change MAC address in windows 7