发布于 

Zero Quantity Maximization

Zero Quantity Maximization

CF 1133 D

题目来源:Codeforces

分析

题目给出两个数组a[]b[],要求对于满足\(c_i = d \cdot a_i + b_i\)的数组c[],当d取何值时c[]中有尽可能多的0

我们只需要假设所有的c[i]都为0,然后求出满足其关系的d,找到出现次数最多的d就可以了。

这题当时做的时候最早没有用map<>,而是使用了排序后手动计算的方法,结果莫名其妙的出现了许多的bug,换成map<>之后就好了。。。此外,这里其实可以不使用Node,而是直接用一个pair<int, int>即可,只需要记住用gcd()将分数化为最简形式即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>

#define MAXN 200100

using namespace std;

struct Node
{
long long a, b;
int k;

void getK()
{
if (this->a * this->b<0)
this->k = -1;
else this->k = 1;
}

const bool operator < (const Node &tmp)const{
return abs(this->b * tmp.a) * this->k < abs(tmp.b * this->a) * tmp.k;
}

}node[MAXN];

map<Node, int> mp;

int main()
{
ios::sync_with_stdio(false);

int n;
cin >> n;

for (int i = 1; i<=n; i++)
cin >> node[i].a;

for (int i = 1; i<=n; i++)
{
cin >> node[i].b;
node[i].getK();
}

int cnt = 0;
for (int i = 1; i<=n; i++)
if (node[i].a==0 && node[i].b==0)
{
cnt ++;
}else if (node[i].a!=0)
{
mp[node[i]]++;
}

int ans = 0;
map<Node, int>::iterator it;
for (it = mp.begin(); it!=mp.end(); it++)
ans = max(ans, it->second);

cout << ans + cnt << endl;
}

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站由 [@Songer](https://blog.songer.xyz/) 创建,使用 Stellar 作为主题。