题目链接:

B. Maximum Value

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题意:

给n个数,要求算出最大的a[i]%a[j]的值,其中a[i]>=a[j];

思路:

可以枚举a[j],然后找出它的所有倍数,然后再在序列中找到小于它且最接近它的数.然后就是这个阶段里面最大的值了;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+20;
const int maxn=1e6+220;
const double eps=1e-12; int a[N],n,vis[maxn]; int main()
{
read(n);
For(i,1,n)read(a[i]);
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]==a[i-1])continue;
int pos=i;
for(int k=2; ;k++)
{
int h=k*a[i];
int l=pos+1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]>=h)r=mid-1;
else l=mid+1;
}
pos=r;
ans=max(ans,a[pos]%a[i]);
if(h>=maxn)break;
}
}
cout<<ans<<endl;
return 0;
}

  

最新文章

  1. mysql datetime查询异常
  2. 自定义列表dl的使用原因和场合
  3. Python中类的特殊方法详解
  4. 用c#写的一个局域网聊天客户端 类似小飞鸽
  5. Android开发数据库三层应用-DataSnap
  6. 138. Copy List with Random Pointer
  7. Objective-C:KVO
  8. HTML5 微信二维码提示框
  9. javascript如何解析json对javascript如何解析json对象并动态赋值到select列表象并动态赋值到select列表
  10. ViewController
  11. windows phone 8.1开发SQlite数据库操作详解
  12. objc直接通过指针访问对象实例变量
  13. 【DWM1000】 code 解密8一 TAG接收blink response 信号
  14. java_web—JSP+Servlet+JavaBean
  15. python 字符串的一些方法
  16. tkinter之Frame
  17. Opengl绘制我们的小屋(一)球体,立方体绘制
  18. spring aop 样例
  19. JS设计模式——5.单体模式(用了这么久,竟全然不知!)
  20. Bezier曲线原理—动态解释

热门文章

  1. ASP.NET Core1.0 带来的新特性
  2. JVM的ClassLoader过程分析
  3. __proto__
  4. CRM 2013 系统设置新功能一:界面自动保存 及 SDK 中 Xrm.Page.data.entity.save
  5. iOS 学习 - 5.UILabel设置行距
  6. iOS-绘图(Quartz2D)的简单使用(原创)
  7. iOS实现三屏复用循环广告[从服务器请求的广告]
  8. Java基础之子类父类属性覆盖
  9. Monyer&#39;s Game 11~15关过关方法
  10. 2.1.12 Next Permutation 下一个字典序数组