标签:des style http color os io for ar
3 2 1 1 2 2 3 3
3HintIf two people stand in one place, they are embracing.
解题思路:告诉你n个点,再告诉你正方形边长,问你这个正方形最多包含多少个点?
先枚举x在范围内的点,O(n)的效率,然后对x在范围的点中,找出y在范围内的点,这个考虑到n不是很大,排序找出最大的即可,否则要用到线段树。
解题代码:
#include <iostream> #include <vector> #include <queue> #include <cstdio> #include <algorithm> using namespace std; const int maxn=1100; int n,l; struct point{ int x,y; point(int x0=0,int y0=0){ x=x0;y=y0; } friend bool operator < (point p1,point p2){ if(p1.x!=p2.x) return p1.x<p2.x; else return p1.y<p2.y; } }p[maxn]; bool cmp(point p1,point p2){ return p1.y<p2.y; } int getCnt(vector <point> v){ sort(v.begin(),v.end(),cmp); queue <int> q; int tmp=0; for(int i=0;i<v.size();i++){ if(!q.empty()){ int s=q.front(); if(v[i].y-v[s].y<=l){ q.push(i); }else{ q.pop(); i--; } }else{ q.push(i); } if(q.size()>tmp) tmp=q.size(); } return tmp; } void solve(){ int ans=0; sort(p,p+n); queue <int> q; for(int i=0;i<n;i++){ if(!q.empty()){ int s=q.front(); if(p[i].x-p[s].x<=l){ q.push(i); }else{ vector <point> v; int qsize=q.size(); while(qsize-- >0){ int s=q.front(); q.pop(); v.push_back(p[s]); q.push(s); } int tmp=getCnt(v); if(tmp>ans) ans=tmp; q.pop(); i--; } }else{ q.push(i); } } vector <point> v; while(!q.empty()){ int s=q.front(); q.pop(); v.push_back(p[s]); } int tmp=getCnt(v); if(tmp>ans) ans=tmp; printf("%d\n",ans); } int main(){ while(scanf("%d%d",&n,&l)!=EOF){ for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); solve(); } return 0; }
HDU 4007 Dave (基本算法-水题),布布扣,bubuko.com
标签:des style http color os io for ar
原文地址:http://blog.csdn.net/a1061747415/article/details/38362337