cf Codeforces Round #544 D. Zero Quantity Maximization

原題:

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,使得ci=dai+bic_i = d·a_i+b_i,使得cic_i爲0的數量最多,輸出最多有多少個cic_i爲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;
}
 

解答:
ci=dai+bic_i = d·a_i+b_i可以看出,要想讓cic_i等於0最多,可以將序列a和b看成給出的x點座標和y點座標,找出在座標系上哪一條直線覆蓋了最多的點。那麼,可以用一個map記錄斜率出現最多的值就是答案,即aibi\frac{a_i}{b_i},別忘了約去aia_ibib_i的最大公約數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章