1133D Zero Quantity Maximization
https://codeforces.com/problemset/problem/1133/D
题意:给定长度为n的数组a和b,问如果选取一个d,那么c[i] = a[i] * d + b[i],令数组c中的0尽可能的多,最多有多少。
思路:从头到尾开始遍历数组,考虑当前b[i]是否为0,如果为0,那么看a[i]是否为0,如果a[i]为0,那么d可以是任意值,如果a[i]不为0,d只能是0。再考虑b[i]不为0,a[i]为0,d无解。最后两个都不为0,构建map统计pair数量即可,这里要对pair进行一个标准化的操作,防止符号问题漏掉统计。
inline void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto& x : a) {
cin >> x;
}
for (auto& x : b) {
cin >> x;
}
constexpr int tag = 1e9 + 2333;
map<pair<int, int>, int> mapp;
for (int i = 0; i < n; ++i) {
if (b[i] == 0) {
if (!a[i]) mapp[{tag, tag}] ++;
else {
mapp[{0, 0}] ++;
}
}
else if (a[i] == 0) {
continue;
}
else{
int g = std::abs(::gcd(a[i], b[i]));
a[i] /= g;
b[i] /= g;
if (b[i] < 0) {
a[i] *= -1;
b[i] *= -1;
}
mapp[{a[i], b[i]}] ++;
}
}
if (mapp.empty()) {
cout << "0\n";
}
else {
int ans = 0;
for (auto [x, y] : mapp) {
if (x != pair{tag, tag}) {
ans = max<int>(ans, y);
}
}
cout << ans + mapp[pair{tag, tag}] << '\n';
}
}

浙公网安备 33010602011771号