Maximum Value(Codeforces_484B)
2015-08-22 15:26
447 查看
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.
The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
3 4 5
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.Sample test(s)
Input
33 4 5
Output
2#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int main() { int n; int a,num[2000100]; scanf("%d",&n); int maxx=-99999; for(int i=1;i<=n;i++) { scanf("%d",&a); num[a]=a; maxx=max(maxx,a); } for(int i=1;i<=maxx*2+1;i++) { if(num[i]!=i) num[i]=num[i-1];//这样处理一下更高效 } int ans=0; for(int i=2;i<maxx;i++) { if(num[i]==i) { for(int j=i*2;j<=maxx*2+1;j+=i)//枚举i的倍数 { if(num[j-1]>i)//与i取余所能得到的最大值只会在比i的倍数小的数中的最大值中出现 ans=max(ans,num[j-1]%i);//(有点绕,譬如有四个数3,4,5,6,i=3,比i的2倍小的数有4,5,最大的是5,所以5%3是当前所得到的最大余数 } } } printf("%d\n",ans); return 0; }
相关文章推荐
- N-Queens
- IOS UI 自定义navigationBar布局
- UIView
- UILable
- hdu 5412 CRB and Queries 求动态区间第k小 树套树
- Hbuilder连接模拟器调试
- IOS UI 代码界面跳转常用方式
- Unique Paths
- iphone键盘的回收 && 键盘弹起遮挡UITextFiled等空间问题解决
- Opensack Issue & Solution: "HypervisorUnavailable: Connection to the hypervisor is broken on host"
- LinkedBlockingQueue多线程测试
- Qt:在Quick中使用QZXing库做二维码扫描
- Burp Suite--攻击web 应用程序的集成平台
- uva 11235 Frequent values RMQ问题
- String 类型基本转换 valueOf
- java String,StringBuffer和StringBuilder
- UIImage的两个方法的内存分析
- view 在父view 拖动不过边界(UIPanGestureRecognizer)
- 算法-QuickSort
- [LeetCode] N-Queens II