• Codesforces 485D Maximum Value


                                                      D. Maximum Value
     

    You are given a sequence a consisting of n integers. Find the maximum possible value of  (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最近的数是什么。就可以过了。

    #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
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    51单片机入门(三)
    51单片机入门笔记(二)
    51单片机入门笔记
    团队项目-需求分析报告
    团队项目-选题报告
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    tomcat的安装和配置
    循环
  • 原文地址:https://www.cnblogs.com/hlmark/p/4081010.html
Copyright © 2020-2023  润新知