洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解題報告
P2862 [USACO06JAN]把牛Corral the Cows
題目描述
Farmer John wishes to build a corral for his cows. Being finicky beasts, they demand that the corral be square and that the corral contain at least C (1 <= C <= 500) clover fields for afternoon treats. The corral‘s edges must be parallel to the X,Y axes.
FJ‘s land contains a total of N (C <= N <= 500) clover fields, each a block of size 1 x 1 and located at with its lower left corner at integer X and Y coordinates each in the range 1..10,000. Sometimes more than one clover field grows at the same location; such a field would have its location appear twice (or more) in the input. A corral surrounds a clover field if the field is entirely located inside the corral‘s borders.
Help FJ by telling him the side length of the smallest square containing C clover fields.
約翰打算建一個圍欄來圈養他的奶牛.作為最挑剔的獸類,奶牛們要求這個圍欄必須是正方 形的,而且圍欄裏至少要有C< 500)個草場,來供應她們的午餐.
約翰的土地上共有C<=N<=500)個草場,每個草場在一塊1x1的方格內,而且這個方格的 坐標不會超過10000.有時候,會有多個草場在同一個方格內,那他們的坐標就會相同.
告訴約翰,最小的圍欄的邊長是多少?
輸入輸出格式
輸入格式:
Line 1: Two space-separated integers: C and N
Lines 2..N+1: Each line contains two space-separated integers that are the X,Y coordinates of a clover field.
輸出格式:
Line 1: A single line with a single integer that is length of one edge of the minimum size square that contains at least C clover fields.
這個題目的數據比較小,我的做法是\(O(N^2*log^2N)\),優化掉一個\(logN\)
先離散化,然後枚舉正方形左下角,分別二分兩個相鄰的邊的變成並更新答案。
事實上這個題難在實現上,感覺代碼不太好寫。
Code:
#include <cstdio>
#include <set>
#include <map>
using namespace std;
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
const int N=504;
struct node
{
int x,y,cnt;
}t[N][N];
int n,rr,cntx,cnty,f[N][N],fx[N],fy[N],ans=0x3f3f3f3f,X[10010],Y[10010];
map <int,map<int,int > > m;
set <int > s1,s2;
bool check0(int i,int j,int R)//第i行第j列為左下角
{
int l=i,r=cnty,d=fx[R]-fx[j];
while(l<r)
{
int mid=l+r+1>>1;
if(fy[mid]-fy[i]>d)
r=mid-1;
else
l=mid;
}
if(f[l][R]-f[i-1][R]-f[l][j-1]+f[i-1][j-1]>=rr)
{
ans=min(ans,d);
return true;
}
else
return false;
}
bool check1(int i,int j,int R)//第i行第j列為左下角
{
int l=j,r=cntx,d=fy[R]-fy[i];
while(l<r)
{
int mid=l+r+1>>1;
if(fx[mid]-fx[j]>d)
r=mid-1;
else
l=mid;
}
if(f[R][l]-f[R][j-1]-f[i-1][l]+f[i-1][j-1]>=rr)
{
ans=min(ans,d);
return true;
}
else
return false;
}
int main()
{
scanf("%d%d",&rr,&n);
int x,y,x0=0,y0=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
m[x][y]++;
s1.insert(x);
s2.insert(y);
}
while(!s1.empty())
{
X[*s1.begin()]=++cntx;
s1.erase(s1.begin());
}
while(!s2.empty())
{
Y[*s2.begin()]=++cnty;
s2.erase(s2.begin());
}
for(map <int,map<int,int > >::iterator it1=m.begin();it1!=m.end();it1++)
{
for(map <int,int >::iterator it2=(it1->second).begin();it2!=(it1->second).end();it2++)
{
x0=X[it1->first],y0=Y[it2->first];
t[x0][y0].cnt=it2->second;
t[x0][y0].x=it1->first;
t[x0][y0].y=it2->first;
}
}
for(int i=1;i<=cnty;i++)
for(int j=1;j<=cntx;j++)
{
f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+t[j][i].cnt;
fx[j]=max(fx[j],t[j][i].x);
fy[i]=max(fy[i],t[j][i].y);
}
for(int i=1;i<=cnty;i++)//枚舉從下往上第幾行
for(int j=1;j<=cntx;j++)//枚舉左下角
{
int l=j,r=cntx;//二分x的長度
while(l<r)
{
int mid=l+r>>1;
if(check0(i,j,mid))
r=mid;
else
l=mid+1;
}
check0(i,j,l);
l=i,r=cnty;//二分y的長度
while(l<r)
{
int mid=l+r>>1;
if(check1(i,j,mid))
r=mid;
else
l=mid+1;
}
check1(i,j,l);
}
printf("%d\n",ans+1);
return 0;
}
代碼寫的的確不聰明
2018.6.20
洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解題報告