51nod 1421 最大MOD值(贪心)

本文介绍了一种算法,用于解决在大量整数中寻找任意两数模值最大的问题。通过对数组进行排序并逆序遍历,利用贪心策略找到满足条件的最大模值对。此算法适用于处理大规模数据集,如包含2e5个1e6级别的数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2e5个1e6的数字,最大两个数的mod值
排序,从后往前,找小于k个a[i]的最大数.
贪心一下就好.

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#ifdef noob
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
#else
#define debug(...)
#endif
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 17;
const int MOD = 998244353;
int a[MAXN];
int main()
{
#ifdef noob
    freopen("Input.txt","r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
    int n;
    cin>>n;
    for (int i = 0; i < n; ++i)
    {
        cin>>a[i];
    }
    sort(a, a+n);
    int ans = 0;
    for (int i = n-1; i > -1; --i)
    {
        if(ans>=a[i]) break;
        for (int j = 1; ; ++j)
        {
            int x = a[lower_bound(a, a+n, j*a[i])-a-1];
            if(x>=a[i]) ans = max(ans,x%a[i]);
            if(x==a[n-1]) break;
        }
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值