zoukankan      html  css  js  c++  java
  • 51Nod 1421

    有一个a数组,里面有n个整数。现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai  aj

    Input
    单组测试数据。
    第一行包含一个整数n,表示数组a的大小。(1 ≤ n ≤ 2*10^5)
    第二行有n个用空格分开的整数ai (1 ≤ ai ≤ 10^6)。
    Output
    输出一个整数代表最大的mod值。
    Input示例
    3
    3 4 5
    Output示例
    2

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    const int MAXN = 2e5 + 10;
    
    int n;
    int a[MAXN];
    
    template <class T>
    inline void scan_d(T &ret)
    {
        char c;
        ret = 0;
        while ((c = getchar()) < '0' || c > '9');
        while (c >= '0' && c <= '9')
        {
            ret = ret * 10 + (c - '0'), c = getchar();
        }
    }
    
    int main(int argc, const char * argv[])
    {
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            scan_d(a[i]);
        }
    
        sort(a, a + n);
    
        int res, tmp, j;
        j = res = tmp = 0;
        for (int i = 1; i < n; i++)
        {
            tmp = a[i] % a[i - 1];
            if (tmp > a[i] % a[j])
            {
                while (tmp > a[i] % a[j])
                {
                    j++;
                }
            }
    
            while (a[i] % a[j + 1] > a[i] % a[j])
            {
                j++;
            }
            tmp = a[i] % a[j];
    
            res = max(res, tmp);
        }
    
        cout << res << endl;
    
        return 0;
    
    }
  • 相关阅读:
    Free DIY Tour_DP
    找出直系亲属_floyd
    Constructing Roads In JGShining's Kingdom_最长上升子序列
    买卖股票_线性dp
    Common Subsequence_公共子序列
    Doing Homework_状态压缩&&位运算
    猴子选大王
    卡片游戏
    Java 2
    Java 1
  • 原文地址:https://www.cnblogs.com/kimsimple/p/7188626.html
Copyright © 2011-2022 走看看