• 1706D1 - Chopping Carrots (Easy Version)


    原题链接:

    Problem - 1706D1 - Codeforces

    题目描述:

    This is the easy version of the problem. The only difference between the versions is the constraints on nn, kk, aiai, and the sum of nn over all test cases. You can make hacks only if both versions of the problem are solved.

    Note the unusual memory limit.

    You are given an array of integers a1,a2,…,ana1,a2,…,an of length nn, and an integer kk.

    The cost of an array of integers p1,p2,…,pnp1,p2,…,pn of length nn is

    max1≤i≤n(⌊aipi⌋)−min1≤i≤n(⌊aipi⌋).max1≤i≤n(⌊aipi⌋)−min1≤i≤n(⌊aipi⌋).

    Here, ⌊xy⌋⌊xy⌋ denotes the integer part of the division of xx by yy. Find the minimum cost of an array pp such that 1≤pi≤k1≤pi≤k for all 1≤i≤n1≤i≤n.

    Input

    The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.

    The first line of each test case contains two integers nn and kk (1≤n,k≤30001≤n,k≤3000).

    The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤a1≤a2≤…≤an≤30001≤a1≤a2≤…≤an≤3000).

    It is guaranteed that the sum of nn over all test cases does not exceed 30003000.

    Output

    For each test case, print a single integer — the minimum possible cost of an array pp satisfying the condition above.

    题目大意:

    给定一个数组a,给定一个k,请你构造一个数组p,使得最大的a[i]/p[i]和最小的a[i]/p[i]之间的差值尽量小,注意p[i]不得大于k。

    解题思路:

    枚举最小值minV,然后我们需要取尽可能大的p[i],使得每个a[i]/p[i]大于等于minV。然后维护答案即可。

     特例:如果给出的k大于数组a的最大值的话,我们可以把整个数组p都取k,那么所有的a[i]/p[i]都为0,所以答案直接输出0即可

    代码(CPP):

    1. #include
    2. using namespace std;
    3. #define endl '\n'
    4. typedef long long ll;
    5. typedef unsigned long long ull;
    6. const int maxn = 3e3 + 10;
    7. const int INF = 0x3fffffff;
    8. int n, a[maxn], p[maxn], k;
    9. /*
    10. 枚举最小值minV,然后我们需要取尽可能大的p[i],使得每个a[i]/p[i]大于等于minV。然后维护答案即可。
    11. 特例:如果给出的k大于数组a的最大值的话,我们可以把整个数组p都取k,那么所有的a[i]/p[i]都为0,所以答案直接输出0即可。
    12. */
    13. int main()
    14. {
    15. ios::sync_with_stdio(false);
    16. cin.tie(0);
    17. cout.tie(0);
    18. cout << fixed;
    19. cout.precision(18);
    20. int t;
    21. cin >> t;
    22. while(t--)
    23. {
    24. cin >> n >> k;
    25. for (int i = 1; i <= n; i++)
    26. {
    27. cin >> a[i];
    28. }
    29. sort(a + 1, a + 1 + n);
    30. if(k > a[n])
    31. cout << 0 << endl;
    32. else
    33. {
    34. int ans = INF;
    35. for (int minV = 1; minV <= a[1]; minV++)
    36. {
    37. int Max = -1, Min = INF;
    38. for (int i = 1; i <= n; i++)
    39. {
    40. p[i] = a[i] / minV;
    41. if (p[i] > k)
    42. p[i] = k;
    43. Max = max(Max, a[i] / p[i]);
    44. Min = min(Min, a[i] / p[i]);
    45. }
    46. ans = min(Max - Min, ans);
    47. }
    48. cout << ans << endl;
    49. }
    50. }
    51. return 0;
    52. }

  • 相关阅读:
    Retrofit2 源码分析
    探索 Sa-Token (三) 权限认证原理
    19、架构-虚拟化容器
    爬取某网站计算机类图书
    【鸿蒙软件开发】ArkUI容器组件之Grid(网格布局)
    Qt---day4---9.20
    API安全风险治理思路、安全架构设计及实践
    Flutter快学快用02 事件循环:Flutter 中代码是如何执行和运行的
    记一次完整的上云经历
    汇凯金业:黄金期货交易时间规则
  • 原文地址:https://blog.csdn.net/qq_45554473/article/details/127872291