lgP2862[USACO06JAN]

链接:lgP2862[USACO06JAN]

题解

  • 500的N,直接枚举左上角和长,时间复杂度$O(N^3)$,可以过,但是很显然长度是可以二分处理的于是复杂度为$O(N^2log_2N)$
  • 注意题目描述,最后答案要+1,并且判断的时候也有坑。wa了一下午,不知道哪里写挂了,一下午都没找出来,最后重写一遍
  • AC。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e3;
struct node{
int x,y;
}a[N];
int n,m,t[N],l,r=1000000;
inline int cmp(node x,node y){return x.x<y.x;}
int ok(int l,int r,int L){
if(r-l+1<m)return 0;
int tp=0;
rep(i,l,r)t[++tp]=a[i].y;
sort(t+1,t+tp+1);
rep(i,m,tp)if(t[i]-t[i-m+1]<=L)return 1;
return 0;
}

int judge(int L){
int tp=1;
rep(i,1,n){
if(a[i].x-a[tp].x>L&&ok(tp,i-1,L))return 1;
while(a[i].x-a[tp].x>L)tp++;
}
return ok(tp,n,L);
}

int main(){
scanf("%d%d",&m,&n);
rep(i,1,n)scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,cmp);
while(l<r){
int mid=l+r>>1;
if(judge(mid))r=mid;
else l=mid+1;
}
printf("%d",l+1);
return 0;
}