CodeForces - 484BMaximum Value(hash优化)

个人心得:周测题目,一题没出,难受得一批。这个题目做了一个半小时还是无限WR,虽然考虑到了二分答案这个点上面了,

奈何二分比较差就想用自己的优化,虽然卡在了a=k*b+c,这里但是后面结束了这样解决还是超时了,看了一下网上的hash,思想一样

但是却优化了很多,服气

题目:

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
3
3 4 5
Output
2
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iomanip>
 6 #include<algorithm>
 7 using namespace std;
 8 #define inf 1<<29
 9 #define infa 2000000+10
10 #define nu 50005
11 int n,m;
12 int book[infa];
13 int main()
14 {
15     scanf("%d",&n);
16     memset(book,-1,sizeof(book));
17     int maxnum;
18     for(int i=0;i<n;i++){
19        scanf("%d",&m);
20         book[m]=m;
21     }
22     for(int i=1;i<infa; i++)
23         if(book[i]!=i)
24         {
25             book[i]=book[i-1];
26         }
27         int sum=0;
28         for(int i=1;i<infa;i++)
29         {
30             if(book[i]==i)
31                 {
32                     for(int j=i*2-1;j<infa;j+=i)
33                         sum=max(sum,book[j]%i);
34                 }
35         }
36            cout<<sum<<endl;
37     return 0;
38 }
View Code

 



posted @ 2017-11-20 19:58  余生漫漫浪  阅读(235)  评论(0编辑  收藏  举报