题目传送门:http://codeforces.com/problemset/problem/484/B
题意:一个数列中找出最大的两个元素取模值
解题思路:先对数列排序,然后对每个元素,针对其倍数元素前面一个元素进行计算比较,取最大值即为答案。
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 200005;
int num[maxn];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &num[i]);
}
sort(num,num+n);
int ans = 0;
for (int i = 0; i < n; i++)
{
if (i && num[i] == num[i-1]) continue;
int k = 2 * num[i];
while (k <= num[n-1])
{
int pos = lower_bound(num,num+n,k) - num;
ans = max(ans,num[pos-1]%num[i]);
k += num[i];
}
ans = max(ans,num[n-1]%num[i]);
}
printf("%d\n",ans);
return 0;
}