题目:
给定长度相等的数组a、b
c[i] = d*a[i] + b[i]
选择一个d使得c中0的个数尽可能多,问最多有多少0.
思路:
考虑当a[i]为0时,只有b[i]也是0才满足条件
当a[i]不等于0时,计算b[i]/a[i]的值,用map记录并求解该键所对应的最大值
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include <bitset> 8 #include<set> 9 #include<map> 10 #include<unordered_map> 11 #include<vector> 12 #include<cmath> 13 #include<string> 14 using namespace std; 15 typedef long long ll; 16 #define Maxn 200005 17 map<long double, ll>mp; 18 ll a[Maxn],b[Maxn]; 19 ll n, res = 0, ans = 0; 20 int main() { 21 cin >> n; 22 for (int i = 0; i < n; i++) { 23 cin >> a[i]; 24 } 25 for (int i = 0; i < n; i++) { 26 cin >> b[i]; 27 // c[i] = (-1) * b[i] / a[i]; 28 } 29 for (int i = 0; i < n; i++) { 30 if (a[i] == 0) { //a[i]为0时,只有b[i]也是0,才能+1 31 if (b[i] == 0) { //直接跳过 32 res++; 33 } 34 continue; 35 } 36 mp[(long double)b[i] / a[i]]++; 37 ans = max(ans, mp[(long double)b[i] / a[i]]); 38 } 39 40 cout << ans + res << endl; 41 return 0; 42 }