首页 > 其他 > 详细

Codesforces 485D Maximum Value

时间:2014-11-07 12:59:36      阅读:318      评论:0      收藏:0      [点我收藏+]
                                                  D. Maximum Value
 

You are given a sequence a consisting of n integers. Find the maximum possible value of bubuko.com,布布扣 (integer remainder of ai divided byaj), 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

 

 

给出 n 个数 a[0] ~ a[n-1] ,要你找出 a[i] % a[j] 最大.

处理一个数组x[i]表示 1~i 离i最近的数是什么。就可以过了。

 

bubuko.com,布布扣
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int mod = (1e9+7);
const int N = 2000010;
bool num[N];
int x[N] , a;
int main()
{
    int n ;
    cin >> n ;
    for( int i = 0 ; i < n ; ++i ) {
        cin >> a;
        num[a] = 1 ;
    }    
    for( int i = 0 ; i <= 2e6 ; ++i ){
        if( num[i] )x[i] = i ;
        else x[i] = x[i-1] ;
    }
    int res = 0  ;
    for( int i = 1 ; i <= 1e6 ; ++i ) if( num[i] ){ 
        for( int j = 2 * i ; j <= 2e6 ; j += i ) {
            res = max( res , ( x[j-1] ) % i );
        }
    }
    cout << res << endl;
}
View Code

 

 

 

Codesforces 485D Maximum Value

原文:http://www.cnblogs.com/hlmark/p/4081010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!