zoukankan      html  css  js  c++  java
  • Maximum Value (二分+思维枚举)

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

    Input

    The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).

    The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).

    Output

    Print the answer to the problem.

    Examples

    Input

    3
    3 4 5
    

    Output

    2
    

    发现纯暴力绝对不行的

    由于由于求的是余数

    因此只需要二分出对于小于每个倍数的最大值即可

    但是看似复杂度似乎很高 实则内层循环求和之后是ln(n)

    所以该算法分复杂度是o(n*log(n)*log(n))

    #include<cstdio>
    #include<map>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    using namespace std;
    int a[200005];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        int MAX=0;
        sort(a+1,a+1+n);
        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 tmp;
                    tmp=lower_bound(a+1,a+1+n,j*a[i])-a;
                    MAX=max(MAX,a[tmp-1]%a[i]);
                }
                MAX=max(MAX,a[n]%a[i]);
            }
           // cout<<MAX<<endl;;
        }
        printf("%d
    ",MAX);
        return 0;
    }
    //caowenbo
    
  • 相关阅读:
    拥抱函数式编程 I
    关于CMS的那点事 I
    常用正规表达式
    javascript source map 的使用
    架构师修炼 后记
    CSS 天坑 I
    架构师修炼 III
    架构师修炼 II
    win10,VM14 安装cnetos6.9 虚拟机黑屏和只有光标闪烁解决办法
    C/C++数组初始化
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852304.html
Copyright © 2011-2022 走看看