码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 484BMaximum Value(hash优化)

时间:2017-11-20 21:56:17      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:orm   scanf   ++   string   cti   ide   col   bsp   iostream   

个人心得:周测题目,一题没出,难受得一批。这个题目做了一个半小时还是无限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

 



CodeForces - 484BMaximum Value(hash优化)

标签:orm   scanf   ++   string   cti   ide   col   bsp   iostream   

原文地址:http://www.cnblogs.com/blvt/p/7867993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!