[CF354C] Vasya and Beautiful Arrays

Description

定义一个数组的美丽度为这个数组所有元素的最大公约数,对于每个元素可以减一个 \(0 \sim k\) 的数,减完以后该元素必须非负,求出这个数组的最大美丽度。

Solution

显然 ans 不会大于最小数,设 ans 等于最小数,循环遍历每个数,如果不符合条件就将 ans 减一继续循环遍历

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int n, k, a[N];

signed main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int ans = *min_element(a + 1, a + n + 1);
    int flag = 1;
    while (flag)
    {
        flag = 0;
        for (int i = 1; i <= n; i++)
            while (a[i] % ans > k)
            {
                ans--;
                flag = 1;
            }
    }
    cout << ans << endl;
}

相关文章:

  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2021-08-27
  • 2021-10-06
  • 2021-11-07
  • 2021-06-22
猜你喜欢
  • 2022-01-20
  • 2021-06-02
  • 2021-10-27
  • 2021-07-05
  • 2022-01-19
  • 2021-06-12
相关资源
相似解决方案