codeforce 1133D. Zero Quantity Maximizationr
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
inputCopy
5
1 2 3 4 5
2 4 7 11 3
outputCopy
2
inputCopy
3
13 37 39
1 2 3
outputCopy
2
inputCopy
4
0 0 0 0
1 2 3 4
outputCopy
0
inputCopy
3
1 2 -1
-6 -12 6
outputCopy
3
Hint
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.
题意:
给出两组数a和b, 另一组数c为ci:=d⋅ai+bi(d为任意实数), 求出d为多少时c中0的个数最多
题解:
简单数学, 推理一下该式子可以得出a/b最多的数量即是. 很显然应该用map来存储一下取最值. 但由于d为任意实数, 这个地方用double会WA, 要再用一下pair来表示分数即可
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 2*1e5+10;
typedef pair<int, int> P; //最简分数
int n, a[maxn], b[maxn];
map<P, int> ma;
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n; i++)
cin >> b[i];
int cnt0 = 0; //a, b均为0的次数
for(int i = 1; i <= n; i++){
if(a[i]==0 && b[i]==0)
cnt0++;
else if(a[i]==0)
continue;
else{
int g = __gcd(a[i], b[i]);
P cur = P(b[i]/g, a[i]/g);
ma[cur]++;
}
}
int ans = 0;
for(map<P, int>::iterator it = ma.begin(); it != ma.end(); it++)
ans = max(it->second, ans);
cout << ans+cnt0 << endl;
return 0;
}