标签:+= Plan uil 二维 return sample over c++ ali
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1352 | Accepted: 565 |
Description
Input
Output
Sample Input
3 4 1 2 2 1 4 1 5 2
Sample Output
4
Hint
|* *Below is one 4x4 solution (C‘s show most of the corral‘s area); many others exist.
| * *
+------
|CCCC
|CCCC
|*CCC*
|C*C*
+------
Source
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 510; int C,n; struct node{ int x,y; }p[maxn]; int xn,yn,rx[maxn],ry[maxn]; int sum[maxn][maxn]; bool cmp1(node a,node b){ return a.x<b.x; } bool cmp2(node a,node b){ return a.y<b.y; } bool check(int k){ for(int a=1,b=1;;a++){ while(rx[b+1]-rx[a]+1<=k&&b<xn) b++; for(int c=1,d=1;;c++){ while(ry[d+1]-ry[c]+1<=k&&d<yn) d++; int ans=sum[b][d]+sum[a-1][c-1]-sum[a-1][d]-sum[b][c-1]; if(ans>=C) return true; if(d==yn) break; } if(b==xn) break; } return false; } int main(){ scanf("%d%d",&C,&n); for(int i=1;i<=n;i++){ scanf("%d%d",&p[i].x,&p[i].y); } sort(p+1,p+n+1,cmp1); xn=1;rx[1]=p[1].x;p[1].x=1; for(int i=2;i<=n;i++){ if(p[i].x!=p[i-1].x) rx[++xn]=p[i].x; p[i].x=xn; } sort(p+1,p+n+1,cmp2); yn=1;ry[1]=p[1].y;p[1].y=1; for(int i=2;i<=n;i++){ if(p[i].y!=p[i-1].y) ry[++yn]=p[i].y; p[i].y=yn; } for(int i=1;i<=n;i++) sum[p[i].x][p[i].y]++; for(int i=1;i<=xn;i++){ for(int j=1;j<=yn;j++){ sum[i][j]+=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]; } } int l=1,r=max(rx[xn],ry[yn]),ans; while(l<=r){ int mid=l+r>>1; if(check(mid)){ ans=mid; r=mid-1; }else{ l=mid+1; } } printf("%d\n",ans); }
标签:+= Plan uil 二维 return sample over c++ ali
原文地址:https://www.cnblogs.com/buerdepepeqi/p/9497623.html