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.

Sample test(s)
input
3
3 4 5
output
2

题意:给一堆数,然后要求找两个数,大的数除小的数求余,求最大的余值是多少

思路:类似筛表法的思想,对于每个aj找ai。对每个aj,就for(p=aj;p<mx;p+=aj) 然后二分一下找小于a[p]且最接近a[p]的数,然后用这个数维护答案。另外一点由于这里的数只有10^6,所以可以不用二分找最靠近的数,用一个数组直接维护就可以了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
const int M = ;
int cas = ; int a[N];
bool vis[M];
int num[M];
int n,mxnum,ans; void run()
{
int x;
mxnum=;
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
for(int i=;i<n;i++)
{
scanf("%d",&x);
if(mxnum<x) mxnum=x;
vis[x]=;
}
for(int i=;i<=mxnum;i++)
{
if(vis[i]) num[i]=i;
else num[i]=num[i-];
}
ans=;
for(int aj=;aj<=mxnum;aj++)
{
if(!vis[aj]) continue;
if(ans<mxnum%aj) ans=mxnum%aj;
for(int p=aj*;p<=mxnum;p+=aj)
{
if(ans < num[p-]%aj)
ans = num[p-]%aj;
}
}
printf("%d\n",ans);
} int main()
{
#ifdef LOCAL
// freopen("case.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
run();
return ;
}

最新文章

  1. SQL 统计两个表的数据,按同一日期分组
  2. linux mint 崩溃
  3. linux进程间通信-管道
  4. Maven配置不成功
  5. Boyer-Moore algorithm
  6. Scrum敏捷精要
  7. ASP.NET的六种验证控件的使用
  8. ADB Server Didn’t ACK ,failed to Start Daemon 解决方法
  9. Java Socket 异常 Connection reset
  10. ZOJ 3469 Food Delivery
  11. JavaScript快速入门(二)——JavaScript变量
  12. git三个区域详解
  13. SourceTree 无法查看组织仓库
  14. Node.js和html数据交互(一) form表单
  15. ---- 关于Android蓝牙搜索到设备的图标显示和设备过滤
  16. Trickbot增加的远程应用程序凭证抓取功能
  17. Python开发——数据类型【字符串】
  18. 安装xcache3.0.3/3.2,为php加速
  19. 大数据学习路线之linux系统基础搭建
  20. Elasticsearch初探(一)

热门文章

  1. java ResultSet获得总行数
  2. python 基础 7.4 os 模块
  3. 搭建sftp服务+nginx代理
  4. EasyRTMP Android安卓手机直播推流摄像头偏暗的问题解决
  5. nexus-2.11.4-01-bundle.tar.gz 下载地址
  6. SQL语句性能优化操作
  7. Linux内核--并发【转】
  8. DubboAdmin平台
  9. ansible playbook学习
  10. JAVA- String类练习