Codeforces Round #544 (Div. 3) D. Zero Quantity Maximization
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?
The first line contains one integer ?n (1≤?≤2⋅1051≤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).
Print one integer — the maximum number of zeroes in array ?c, if you choose ?d optimally.
5 1 2 3 4 5 2 4 7 11 3
2
3 13 37 39 1 2 3
2
4 0 0 0 0 1 2 3 4
0
3 1 2 -1 -6 -12 6
3
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; }