zoukankan      html  css  js  c++  java
  • [CF354C] Vasya and Beautiful Arrays

    [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;
    }
    
  • 相关阅读:
    c++函数学习-关于c++函数的林林总总
    STL学习笔记(七) 程序中使用STL
    STL学习笔记(六) 函数对象
    本学期总结与课程建议
    12.19
    12.18Tomcat相关知识
    12.17
    12.16
    12.15
    12.14
  • 原文地址:https://www.cnblogs.com/mollnn/p/14367286.html
Copyright © 2011-2022 走看看