-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaddsqure.cpp
91 lines (78 loc) · 1.73 KB
/
addsqure.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
// Function to count all the possible
// squares with given lines parallel
// to both the X and Y axis
int numberOfSquares(int X[], int Y[],
int N, int M)
{
// Stores the count of all possible
// distances in X[] & Y[] respectively
unordered_map<int, int> m1, m2;
int i, j, ans = 0;
// Find distance between all
// pairs in the array X[]
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
int dist = abs(X[i] - X[j]);
// Add the count to m1
m1[dist]++;
}
}
// Find distance between all
// pairs in the array Y[]
for (i = 0; i < M; i++) {
for (j = i + 1; j < M; j++) {
int dist = abs(Y[i] - Y[j]);
// Add the count to m2
m2[dist]++;
}
}
// Find sum of m1[i] * m2[i]
// for same distance
for (auto i = m1.begin();
i != m1.end(); i++) {
// Find current count in m2
if (m2.find(i->first)
!= m2.end()) {
// Add to the total count
ans += 1;
}
}
// Return the final count
return ans;
}
bool found(int Y[],int i,int m){
int l=0,u=m-1,mid;
while(l<=u){
mid=(l+u)/2;
if(Y[mid]==i)return true;
else if(Y[mid]<i)l=mid+1;
else u=mid-1;
}return false;
}
// Driver Code
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
int w,h,n,m;
cin>>w>>h>>n>>m;
vector<int> A;
int X[n];
for(int i=0;i<n;i++)cin>>X[i];
int Y[m+1];
for(int j=0;j<m;j++)cin>>Y[j];
sort(X,X+n);sort(Y,Y+m);
int x=X[n-1]-X[0];
x+=Y[m-1];
A.push_back(numberOfSquares(X,Y,n,m+1));
for(int i=1;i<=h;i++){
if(!found(Y,i,m))
{
Y[m]=i;
A.push_back(numberOfSquares(X, Y, n, m+1));
}
}cout<<*max_element(A.begin(), A.end());
return 0;
}