Codeforces div3 Zero Quantity Maximization (map+pair存储分数)

You are given two arrays
a
a
and
b
b
, each contains
n
n
integers.
You want to create a new array
c
c
as follows: choose some real (i.e. not necessarily integer) number
d
d
, and then for every
i∈[1,n]
i∈[1,n]
let
c
i
:=d⋅
a
i
+
b
i
ci:=d⋅ai+bi
.
Your goal is to maximize the number of zeroes in array
c
c
. What is the largest possible answer, if you choose
d
d
optimally?
Input
The first line contains one integer
n
n
(
1≤n≤2⋅
10
5
1≤n≤2⋅105
) — the number of elements in both arrays.
The second line contains
n
n
integers
a
1
a1
,
a
2
a2
, …,
a
n
an
(

10
9

a
i

10
9
−109≤ai≤109
).
The third line contains
n
n
integers
b
1
b1
,
b
2
b2
, …,
b
n
bn
(

10
9

b
i

10
9
−109≤bi≤109
).
Output
Print one integer — the maximum number of zeroes in array
c
c
, if you choose
d
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
d=−2
d=−2
.
In the second example, we may choose
d=−
1
13

d=−113
.
In the third example, we cannot obtain any zero in array
c
c
, no matter which
d
d
we choose.
In the fourth example, we may choose
d=6
d=6
.
问题链接: http://codeforces.com/contest/1133/problem/D
问题简述: 给定一个a[],b[],求出最多数量的d令a[]*d+b[]=0
问题分析: 用map存储分数:求出a[]以及b[]最大公约数,各自除与gcd,求出最简分数再用pair存储。(开始用double,结果WA37…最后查了一下网上有人用long double居然过了)
注意分a[]==0以及b[]==0的情况。具体可看代码及注释
AC通过的C++语言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

const int N=200005;
pair<int,int> pa;//存储分数//double居然还不行,tm好像有两个不同的数double精度一致
map<pair<int,int>,int> vis;//存分数d以及该分数的个数
int a[N],b[N];

int getgcd(int a,int b,int &x,int &y)//扩展欧几里得求最大公约数
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int gcd=getgcd(b,a%b,x,y);
    int x2=x,y2=y;
    x=y2;
    y=x2-(a/b)*y2;
    return gcd;
}

int main()
{
    int n,ans=0,flag=0;
    cin>>n;
    int g=n;
    for(int i=1;i<=n;i++)//输入a[i]
        cin>>a[i];
    for(int i=1;i<=n;i++)//输入b[i]
        cin>>b[i];
    for(int i=1;i<=n;i++)
    {
        int x=0,y=0;//x,y初始化记得先写
        if(a[i]==0&&b[i]!=0) continue;
        else if(a[i]!=0&&b[i]==0)//当a[i]不等于0,b[i]等于0时,只有d等于0才能使c为0
        {
            vis[make_pair(0,0)]++;//make_pair更稳
            continue;
        }
        else if(a[i]==0&&b[i]==0)
        {
            flag++;//当a[i]与b[i]都等于0时,不管d是多少都能令c为0
            continue;
        }
        int gcd=getgcd(a[i],b[i],x,y);//求出最小公约数再以pair形式存储分数
        int f1=a[i]/gcd,f2=b[i]/gcd;
        vis[make_pair(f1,f2)]++;
    }
    map<pair<int,int>,int>::iterator iter;//迭代器iter
    for(iter=vis.begin();iter!=vis.end();iter++)//遍历map中所有的元素
        ans=max(ans,iter->second);//找到最多d的数量
    cout<<ans+flag<<endl;//输出记得加上flag数
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值