原题:
D. Zero Quantity Maximization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two arrays a
and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i∈[1,n] let ci:=d⋅ai+bi.
Your goal is to maximize the number of zeroes in array c
. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n(1≤n≤2⋅10^5) — the number of elements in both arrays.
The second line contains nintegers a1, a2, …, an (−10^9≤ai≤10 ^9).
The third line contains n
integers b1, b2, …, bn (−10^9≤bi≤10 ^9
).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d=−2.
In the second example, we may choose d=−1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d=6.
中文:
给你两个长度为N的序列,a和b,现在让你找到一个数d,使得 c i = d ⋅ a i + b i c_i = d·a_i+b_i ci=d⋅ai+bi,使得 c i c_i ci为0的数量最多,输出最多有多少个 c i c_i ci为0。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=200005;
int a[maxn],b[maxn];
int n;
map<pii,int> mp;
int gcd(int a,int b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n)
{
mp.clear();
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
int ans=0,c,tmp=0;
for(int i=1;i<=n;i++)
{
if(a[i]==0&&b[i]==0)
{
tmp++;
continue;
}
if(a[i]==0)
{
continue;
}
if(b[i]==0)
{
ans=max(ans,++mp[make_pair(0,0)]);
continue;
}
c=gcd(a[i],b[i]);
a[i]/=c;
b[i]/=c;
ans=max(ans,++mp[make_pair(a[i],b[i])]);
}
cout<<ans+tmp<<endl;
}
return 0;
}
解答:
从
c
i
=
d
⋅
a
i
+
b
i
c_i = d·a_i+b_i
ci=d⋅ai+bi可以看出,要想让
c
i
c_i
ci等于0最多,可以将序列a和b看成给出的x点坐标和y点坐标,找出在坐标系上哪一条直线覆盖了最多的点。那么,可以用一个map记录斜率出现最多的值就是答案,即
a
i
b
i
\frac{a_i}{b_i}
biai,别忘了约去
a
i
a_i
ai和
b
i
b_i
bi的最大公约数。