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):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e3 + 10;
const int INF = 0x3fffffff;
int n, a[maxn], p[maxn], k;

/*
    枚举最小值minV,然后我们需要取尽可能大的p[i],使得每个a[i]/p[i]大于等于minV。然后维护答案即可。
    特例:如果给出的k大于数组a的最大值的话,我们可以把整个数组p都取k,那么所有的a[i]/p[i]都为0,所以答案直接输出0即可。
*/

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        sort(a + 1, a + 1 + n);
        if(k > a[n])
            cout << 0 << endl;
        else
        {
            int ans = INF;
            for (int minV = 1; minV <= a[1]; minV++)
            {
                int Max = -1, Min = INF;
                for (int i = 1; i <= n; i++)
                {
                    p[i] = a[i] / minV;
                    if (p[i] > k)
                        p[i] = k;
                    Max = max(Max, a[i] / p[i]);
                    Min = min(Min, a[i] / p[i]);
                }
                ans = min(Max - Min, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值