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⋅105
) — the number of elements in both arrays.
The second line contains n
integers a1, a2, …, an (−109≤ai≤109
).
The third line contains n
integers b1, b2, …, bn (−109≤bi≤109
).
Output
Print one integer — the maximum number of zeroes in array c
, if you choose d
optimally.
Examples
Input
Copy
5
1 2 3 4 5
2 4 7 11 3
Output
Copy
2
Input
Copy
3
13 37 39
1 2 3
Output
Copy
2
Input
Copy
4
0 0 0 0
1 2 3 4
Output
Copy
0
Input
Copy
3
1 2 -1
-6 -12 6
Output
Copy
3
Note
In the first example, we may choose d=−2
.
In the second example, we may choose d=−113
.
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
.
题意:找一个数x ,使a[i]*x+b[i]==0,x相同的情况下最多有多少组符合条件
思路:我一开始想的是把a[i],b[i]合成一个分数,然后二分排序顺便记录出现的次数,最后找出现最多的。。。没用这个,之后用MultiSet来写这个,结果超时了(2e5的数据。。。),之后去找了大佬的博客才知道可以用Map来写(本人并不会map),这样对于分数的记录就比较好了
坑点:注意0 0的组合,这个是万能的
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn=2e5+10;
const int INF=0x7f7f7f7f;
typedef pair<int,int> p;
int n,num1[maxn],num2[maxn];
map<p,int> sum;
map<p,int>::iterator it;
int gcd(int x,int y)
{
if (y==0) return x;
int m=x%y;
return gcd(y,m);
}
int main()
{
cin>>n;
for (int i=1;i<=n;i++)
scanf("%d",&num1[i]);
for (int i=1;i<=n;i++)
scanf("%d",&num2[i]);
int ans=0;
for (int i=1;i<=n;i++)
if (num1[i]==0&&num2[i]==0)
ans++;
else if (num1[i]==0) continue;
else
{
int x=gcd(num1[i],num2[i]);
p k=p(num2[i]/x,num1[i]/x);
sum[k]++;
}
int m=0;
for (it=sum.begin();it!=sum.end();it++)
m=max(m,it->second);
cout<<ans+m<<endl;
return 0;
}