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
  1. 3
    3 4 5
Output
  1. 2
  2.  
  3. 题意:求aj%ai的最大值,其中j>i;
    分析:每次寻找小于k*aj的最大值,类似于素数筛,例如n=5时,3 4 5 6 7 ,我们如果查找对3取模的最大值,毫无疑问就是找到32*3中间的最大值;
  4.  
  5. AC代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<cmath>
  7. #define N 2*100000+10
  8. const int maxn=+;
  9. typedef long long ll;
  10. using namespace std;
  11. int a[N];
  12. int n;
  13. int C(int x){
  14. int w=x,ans=;
  15. while (w<a[n-]){
  16. w+=x;
  17. int k=lower_bound(a,a+n,w)-a;
  18. if (k==) continue;
  19. else k--;
  20. if (a[k]<=x) continue;
  21. ans=max(ans,a[k]%x);
  22. }
  23. return ans;
  24. }
  25. int main(){
  26. ios::sync_with_stdio(false);
  27. int ans=;
  28. scanf("%d",&n);
  29. for (int i=;i<n;i++) scanf("%d",&a[i]);
  30. sort(a,a+n);
  31. for (int i=n-;i>=;i--){
  32. if (i<n-&&a[i]==a[i+])
  33. continue;
  34. if (ans>=a[i]-)
  35. break;
  36. ans=max(ans,C(a[i]));
  37. }
  38. printf("%d\n",ans);
  39. return ;
  40. }
  1.  

Maximum Value(CodeForces - 484B)的更多相关文章

  1. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  2. codeforces 484B B. Maximum Value(二分)

    题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Codeforces 484B Maximum Value(排序+二分)

    题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...

  4. CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)

    B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. CodeForces 484B Maximum Value

    意甲冠军: a序列n(2*10^5)数字  问道a[i]>=a[j]如果是  a[i]%a[j]最大值是多少 思路: 感觉是一道挺乱来的题-- 我们能够将ans表示为a[i]-k*a[j]  这 ...

  6. codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

    题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...

  7. CodeForces 484B 数学 Maximum Value

    很有趣的一道题,题解戳这. #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  8. Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  9. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

随机推荐

  1. win10 python 3.7 pip install tensorflow

    环境: ide:pyCharm 2018.3.2 pyhton3.7 os:win10 64bit 步骤: 1.确认你的python有没有装pip,有则直接跳2.无则cmd到python安装目录下ea ...

  2. 创建Maven项目时Maven中的GroupID和ArtifactID的意思

    GroupID 是项目组织中唯一的标识符,对应Java包结构,在项目中看到的是main目录里java的目录结构. ArtifactID是项目的唯一的标识符,实际对应项目的名称(就是idea中工程的名字 ...

  3. linux查看显卡

    查看 nvidia 显卡 $ lspci | grep -i nvidia 02:00.0 3D controller: nVidia Corporation Device 1023 (rev a1) ...

  4. org.springframework.web.util.UriComponentsBuilder

    import org.springframework.web.util.UriComponentsBuilder; UriComponentsBuilder.fromHttpUrl("htt ...

  5. Servlet&JSP复习笔记 01

    1. Servlet 含义:服务器端的小程序,它只是服务器中的一部分. Servlet Little 标准:Sun公司制定的一种用来扩展Web服务器功能的组件规范. a. 扩展web服务器功能:扩展w ...

  6. Opencv笔记(三)——视频的获取及保存

    一.利用摄像头获取视频 我们经常需要使用摄像头捕获实时图像.OpenCV 为这中应用提供了一个非常简单的接口.让我们使用摄像头来捕获一段视频,并把它转换成灰度视频显示出来.了获取视频,你应该创建一个 ...

  7. ANT下载和配置 IDEA

    1.下载地址大全: http://archive.apache.org/dist/ant/binaries/ jdk与ant版本有对应关系,目前知道: jdk1.7与ant1.10不兼容,1.7必须用 ...

  8. hibernate中session.flush()

    flush()session flush在commit之前默认都会执行, 也可以手动执行,他主要做了两件事: 1) 清理缓存. 2) 执行SQL. flush: Session 按照缓存中对象属性变化 ...

  9. CorsConfig

    package org.linlinjava.litemall.core.config; import org.springframework.context.annotation.Bean; imp ...

  10. 吴裕雄--天生自然python学习笔记:python 用 Tesseract 识别验证码

    用 Selenium 包实现网页自动化操作的案例中,发现很多网页都因 需输入图形验证码而导致实验无法进行 . 解决的办法就是对验证码进行识别 . 识 别的方法之 一 是通过图形处理包将验证码的大部分背 ...