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

 



转载于:https://www.cnblogs.com/blvt/p/7867993.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值