Codeforces Round #544 (Div. 3) 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 𝑖[1,𝑛]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𝑛21051≤n≤2⋅105) — the number of elements in both arrays.

The second line contains 𝑛n integers 𝑎1a1, 𝑎2a2, ..., 𝑎𝑛an (109𝑎𝑖109−109≤ai≤109).

The third line contains 𝑛n integers 𝑏1b1, 𝑏2b2, ..., 𝑏𝑛bn (109𝑏𝑖109−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 𝑑=2d=−2.

In the second example, we may choose 𝑑=113d=−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 𝑑=6d=6.

 

题目不难,更多的是关于心态的反思。

在比赛中遇到问题时,不应该慌乱,应该仔细再读一读题,在纸上把自己的思路写出来,检查有没有错误,不要想当然。

 

#include "bits/stdc++.h"

using namespace std;
const int maxn = 2e5 + 100;
typedef long long ll;

struct node {
    ll a, b;
};

bool operator<(node a, node b) {
    if (a.a != b.a) return a.a < b.a;
    return a.b < b.b;
}

map<node, ll> mp;

ll a[maxn], b[maxn];


int main() {
    //freopen("input.txt", "r", stdin);
    ll n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    ll gcd;
    node temp;
    ll tot = 0;
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
        if (a[i] == 0 && b[i] != 0) {
            continue;
        } else if (a[i] != 0 && b[i] == 0) {
            temp.a = 0;
            temp.b = 0;
        } else if (a[i] == 0 && b[i] == 0) {
            tot++; //比赛时忽视了这种情况。。。
            continue;
        } else {
            gcd = __gcd(a[i], b[i]);
            a[i] /= gcd;
            b[i] /= gcd;
            if (a[i] < 0) {
                a[i] *= -1;
                b[i] *= -1;
            }
            temp.a = a[i];
            temp.b = b[i];
            //temp.a = min(a[i], b[i]);  //这样写会产生错误,比如a=3,b=4与a=4,b=3会归到一种情况,只需要在前面统一a[i]的符号就足够了
            //temp.b = max(a[i], b[i]);
        }
        mp[temp]++;
    }
    map<node, ll>::iterator it;
    it = mp.begin();
    ll ans = 0;
    while (it != mp.end()) {
        ans = max(ans, it->second);
        it++;
    }
    cout << ans + tot << endl;
    return 0;
}

 

posted @ 2019-03-08 14:41  Albert_liu  阅读(382)  评论(0编辑  收藏  举报