Codeforces 484/B Maximum Value

本文介绍了一种算法,用于解决给定整数序列中寻找最大可能的余数值问题。通过二分查找和排序,该算法有效地计算了任意两个元素相除的余数最大值。

文章目录

Maximum Value

You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of a i _i i divided by a j _j j), where 1 ≤ i, j ≤ n and a i _i i ≥ a j _j j.

Input
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·10 5 ^5 5).

The second line contains n space-separated integers a i (1 ≤ a i _i i ≤ 10 6 ^6 6).

Output
Print the answer to the problem.

Examples
inputCopy

3
3 4 5

outputCopy

2

二分出对于小于每个倍数的最大值

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
int a[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        if(i==1||a[i]!=a[i-1])
        {
            for(int j=2;j*a[i]<=a[n];j++)
            {
                int t=lower_bound(a+1,a+1+n,j*a[i])-a;
                ans=max(ans,a[t-1]%a[i]);
            }
            ans=max(ans,a[n]%a[i]);
        }
    }
    printf("%d\n",ans);
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸愉信奥

谢谢亲的支持,我会继续努力啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值