题目:Twinkle Twinkle Little Star
题意:就是给n个点的坐标,然后在这个图形中找出一个边长最小的正方形,要求正方形的边平行于坐标轴且覆盖的点大于等于k个。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 200010;
const int INF = 999999999;
struct Point
{
int x,y;
bool operator < (const Point &a) const
{
if(x!=a.x) return x < a.x;
return y < a.y;
}
};
Point p[N];
int n,k,y[N];
int judge(int len)
{
int pre = -INF;
for(int i=0,j;i<n;i++)
{
if(pre!=p[i].x)
{
pre = p[i].x;
int cnt = 0;
for(j=i;j<n;j++)
{
if(p[j].x>=pre&&p[j].x<=pre+len)
y[cnt++] = p[j].y;
else break;
}
sort(y,y+cnt);
if(cnt<k) continue;
int pre1 = -INF;
for(int r=0;r+k-1<cnt;r++)
{
if(pre1!=y[r])
{
pre1 = y[r];
int up = upper_bound(y,y+cnt,pre1+len)-y-r;
if(up>=k) return 1;
}
}
}
}
return 0;
}
int main()
{
int t=1;
while(~scanf("%d%d",&n,&k))
{
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p,p+n);
int R = max(p[n-1].x-p[0].x,p[n-1].y-p[0].y);
int L = 0;
int ret = 0;
while(L<=R)
{
int mid = (L+R)>>1;
if (judge(mid))
{
ret = mid;
R = mid-1;
}
else L = mid+1;
}
printf("Case %d: %d\n",t++,ret);
}
return 0;
}
标签:return,635,Point,int,mid,NEFU,枚举,const,include From: https://blog.51cto.com/u_16146153/6388583