Maximum Value(哈希)

时间:2023-02-04 03:17:55

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

Hash记录输入的数值,Dp[i]记录输入中距离i最近的数值(不包括本身)

这里写代码片#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin) using namespace std; typedef long long LL; const int MAX = 2*1e6+10; const int R =1e6; int Dp[MAX]; int n; int a[MAX]; bool Hash[MAX]; int main()
{
int n;
memset(Hash,false,sizeof(Hash));
scanf("%d",&n);
int Min=MAX,Max=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Hash[a[i]]=true;
Min=min(Min,a[i]);
Max=max(Max,a[i]);
}
for(int i=Min;i<MAX;i++)
{
if(Hash[i-1])
{
Dp[i]=i-1;
}
else
{
Dp[i]=Dp[i-1];
}
}
int ans=0;
for(int i=Min;i<=R;i++)
{
if(Hash[i])
{
for(int j=i*2;;j+=i)
{
if(Dp[j]<i)
{
continue;
}
ans=max(Dp[j]%i,ans);
if(Dp[j]==Max)
{
break;
}
}
}
}
printf("%d\n",ans);
return 0;
}