Codeforces 484B Maximum Value——思维

来源:互联网 发布:jquery.rotate.js 编辑:程序博客网 时间:2024/06/08 09:13
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 2e5+5;int n, maxa, a[maxn];int main() {    scanf("%d", &n);    maxa = 0;    for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); maxa = max(maxa, a[i]); }    n = unique(a+1, a+1+n)-(a+1);    sort(a+1, a+1+n);    int ans = 0;    for (int j = n; j >= 1; j--) {        if (ans >= a[j]-1) break;        int temp = a[j];        while (temp <= maxa) {            temp += a[j];            int i = lower_bound(a+1, a+1+n, temp) - a;            if (i == 1) continue;            else i--;            ans = max(ans, a[i] % a[j]);        }    }    printf("%d\n", ans);    return 0;}