link:
https://www.acwing.com/problem/content/123/
Meaning of the questions:
Farmer John wanted to build a corral for his cows.
These critical requirements corral animals must be square and must contain at least clover C units, to put out as their afternoon tea.
Corral edges must be parallel to the X, Y axis.
John's land contains a total of N unit clover, clover per unit land area is located within a 1 x 1, the location area represented by the lower left corner coordinates, and a corner of the zone X, Y coordinates are integers in the range 1 to 10000 or less.
Clover plurality of units may be located within the same region of a 1 x 1, and for this reason, the next input, may occur once in the same area coordinates.
Only one of the region located entirely repaired corral, only I think clover in this region in the corral.
The case where you help John calculate, can contain at least C unit area clover, minimum side length of the corral is.
Ideas:
After a two-dimensional discrete and do some prefixes and again to the query can be reduced to about 500 ^ 2 * log (1000).
Code:
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int x, y;
int node;
}node[510];
int Map[10000][10000];
int Number[2010];
int c, n, pos;
bool Check(int len)
{
for (int x1 = 1, x2 = 1;x2 <= pos;x2++)
{
while (Number[x2]-Number[x1]+1 > len)
x1++;
for (int y1 = 1, y2 = 1;y2 <= pos;y2++)
{
while (Number[y2]-Number[y1]+1 > len)
y1++;
if (Map[x2][y2]-Map[x2][y1-1]-Map[x1-1][y2]+Map[x1-1][y1-1] >= c)
return true;
}
}
return false;
}
int main()
{
scanf("%d %d", &c, &n);
int x, y;
pos = 0;
for (int i = 1;i <= n;i++)
{
scanf("%d%d", &node[i].x, &node[i].y);
Number[++pos] = node[i].x;
Number[++pos] = node[i].y;
}
sort(Number+1, Number+1+2*n);
pos = unique(Number+1, Number+1+2*n)-(Number+1);
for (int i = 1;i <= n;i++)
{
node[i].x = lower_bound(Number+1, Number+1+pos, node[i].x)-Number;
node[i].y = lower_bound(Number+1, Number+1+pos, node[i].y)-Number;
Map[node[i].x][node[i].y]++;
}
for (int i = 1;i <= pos;i++)
{
for (int j = 1;j <= pos;j++)
Map[i][j] = Map[i][j]+Map[i-1][j]+Map[i][j-1]-Map[i-1][j-1];
}
int l = 1, r = 10000;
int res = 10000;
while (l < r)
{
// cout << l << ' ' << r << endl;
int mid = (l+r)/2;
if (Check(mid))
{
r = mid;
}
else
l = mid+1;
}
// cout << r << endl;
printf("%d\n", r);
return 0;
}
/*
9 9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
1000 1000
*/