#544 (Div. 3) D. Zero Quantity Maximization

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kwong_young/article/details/88369507

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 n integers.

You want to create a new array c c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i [ 1 , n ] i\in[1,n] let c i : = d a i + b i c_i:=d⋅a_i+b_i .

Your goal is to maximize the number of zeroes in array c c . What is the largest possible answer, if you choose d d optimally?

Input

The first line contains one integer n n ( 1 n 2 1 0 5 ) (1≤n≤2⋅10^5) — the number of elements in both arrays.

The second line contains n
integers a 1 , a 2 , . . . , a n ( 1 0 9 a i 1 0 9 ) . a_1, a_2, ..., a_n (−10^9≤ai≤10^9).

The third line contains n n
integers b 1 , b 2 , . . . , b n ( 1 0 9 b i 1 0 9 ) . b_1, b_2, ..., b_n (−10^9≤bi≤10^9).

Output

扫描二维码关注公众号,回复: 6108118 查看本文章

Print one integer — the maximum number of zeroes in array c c , if you choose d optimally.

Examples

Input
3
13 37 39
1 2 3
Output
2

题目大意

给你数组A和数组B, 让你找到任意实数d使得,尽可能多的 d a i + b i = 0 d⋅a_i+b_i = 0 .成立。
本来我是直接用 b i b_i 除以 a i a_i , 得到一个 d o u b l e double , 然后用 m a p map 计数。但是精度问题,不可以这样做。
所以必须想办法保存这个状态。观察,要使 d a i + b i = 0 d⋅a_i+b_i = 0 成立,固定d不变。 那么对于 d a i k + b i k = 0 k d⋅a_i⋅k+b_i⋅k = 0,k 属于任意数都成立。
从这里我们看出来 g c d gcd 了,将 a i b i a_i、b_i 除以 g c d gcd 后组合成 p a i r pair ,再用 m a p map 统计即可。

注意对于每组 a i b i a_i、b_i 有四种情况

  1. a i = 0 b i 0 a_i = 0,b_i \ne 0 此时,数据无效
  2. a i = 0 b i = 0 a_i = 0,b_i = 0 此时, d d 可以为任意数,一定可以入选
  3. a i 0 b i = 0 a_i \ne 0,b_i = 0 此时, d d 仅可为 0 0
  4. a i 0 b i 0 a_i \ne 0,b_i \ne 0 此时,需要除以最小公倍数,进行统计
#include<bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int a[MAX];
int ans; 
int s, t;
map<pair<int, int>, int> mp;
int gcd(int x, int y){
	return !(x%y) ? y : gcd(y, x % y);
}
int main(){
	int n; cin >> n;
	ans = s = t = 0;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int temp;
	for(int i = 0; i < n; i++){
		cin >> temp;
		if(!temp && !a[i]) s++;
		else if(!temp) t++;
		else if(!a[i]) continue;
		else{
			if(a[i] < 0){
				a[i] = -a[i];
				temp = -temp;
			}
			int gd = gcd(a[i], abs(temp));
			pair<int, int> p = make_pair(a[i]/gd, temp/gd);
			mp[p]++;
			ans = max(ans, mp[p]); 
		}
	}
	ans = max(t, ans);
	cout << ans + s << endl;
	
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Kwong_young/article/details/88369507
今日推荐