• Maximum Value(CodeForces


    Maximum Value

    Time limit 1000 ms

    Memory limit 262144 kB

    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.

    Example

    Input
    3
    3 4 5
    Output
    2

    题意:求aj%ai的最大值,其中j>i;
    分析:每次寻找小于k*aj的最大值,类似于素数筛,例如n=5时,3 4 5 6 7 ,我们如果查找对3取模的最大值,毫无疑问就是找到3到2*3中间的最大值;

    AC代码:
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define N 2*100000+10
    const int maxn=1000000+10;
    typedef long long ll;
    using namespace std;
    int a[N];
    int n;
    int C(int x){
        int w=x,ans=0;
        while (w<a[n-1]){
           w+=x;
           int k=lower_bound(a,a+n,w)-a;
           if (k==0) continue;
           else k--;
           if (a[k]<=x) continue;
           ans=max(ans,a[k]%x);
        }
        return ans;
    }
    int main(){
        ios::sync_with_stdio(false);
        int ans=0;
        scanf("%d",&n);
        for (int i=0;i<n;i++)  scanf("%d",&a[i]);
        sort(a,a+n);
        for (int i=n-1;i>=0;i--){
             if (i<n-1&&a[i]==a[i+1])
              continue;
             if (ans>=a[i]-1)
               break;
             ans=max(ans,C(a[i]));
        }
       printf("%d
    ",ans);
        return 0;
    }
     
  • 相关阅读:
    S02_CH15_ AXI_OLED 实验
    S02_CH14_ EMIO_OLED 实验
    S02_CH13_ AXI_PWM 实验
    S02_CH12_ AXI_Lite 总线详解
    【第二季】CH11_ ZYNQ软硬调试高级技巧
    bzoj3028 食物
    Hdu3579 Hello Kiki
    bzoj4872 [Shoi2017]分手是祝愿
    bzoj4873 [Shoi2017]寿司餐厅
    省选模拟赛 LYK loves rabbits(rabbits)
  • 原文地址:https://www.cnblogs.com/lisijie/p/8424744.html
Copyright © 2020-2023  润新知