CodeForces - 1250 J. The Parade(二分)

题目链接:https://codeforces.com/problemset/problem/1250/J
J. The Parade

The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into k rows, and all rows will contain the same number of soldiers.

Of course, not every arrangement of soldiers into k rows is suitable. Heights of all soldiers in the same row should not differ by more than 1. The height of each soldier is an integer between 1 and n.

For each possible height, you know the number of soldiers having this height. To conduct a parade, you have to choose the soldiers participating in it, and then arrange all of the chosen soldiers into k rows so that both of the following conditions are met:

each row has the same number of soldiers,
no row contains a pair of soldiers such that their heights differ by 2 or more.

Calculate the maximum number of soldiers who can participate in the parade.

Input
The first line contains one integer t (1≤t≤10000) — the number of test cases. Then the test cases follow.

Each test case begins with a line containing two integers n and k (1≤n≤30000, 1≤k≤1e12) — the number of different heights of soldiers and the number of rows of soldiers in the parade, respectively.

The second (and final) line of each test case contains n integers c1, c2, …, cn (0≤ci≤1e12), where ci is the number of soldiers having height i in the Berland Army.

It is guaranteed that the sum of n over all test cases does not exceed 30000.

Output
For each test case, print one integer — the maximum number of soldiers that can participate in the parade.

Example

input
5
3 4
7 1 13
1 1
100
1 3
100
2 1
1000000000000 1000000000000
4 1
10 2 11 1

output
16
100
99
2000000000000
13

Note
Explanations for the example test cases:

  1. the heights of soldiers in the rows can be: [3,3,3,3], [1,2,1,1], [1,1,1,1], [3,3,3,3] (each list represents a row);
  2. all soldiers can march in the same row;
  3. 33 soldiers with height 1 in each of 3 rows;
  4. all soldiers can march in the same row;
  5. all soldiers with height 2 and 3 can march in the same row.

题目大意:为了准备阅兵,要把一些士兵分成 k 排,但是需要满足一个条件,就是同一排的所有士兵的身高差不能超过1,然后让我们计算可以参加阅兵的最大士兵人数。

思路:看完题目大意以后,很明显可以用二分来解决,因为题目里给的 k 这个排数很大,又因为每一排站的人数是一样的,所以我们可以用二分来枚举每一排可以站多少人,然后判断一下是否满足。

注意:数据很大,记得开long long。还需要注意如果总人数比题目给的排数还小的话,就输出0,这里需要判断一下。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f

ll n,k;
ll a[30010],b[30010];

int judge(ll x)
{
    ll ans=0,s=0;
    for(int i=0;i<n;i++)
    {
        if(s+a[i]<x)
            s=0;
        ans+=(s+a[i])/x;
        s=(s+a[i])%x;
    }
    if(ans>=k) return 1;
    else return 0;
}

void binary_Search(ll right)
{
    ll left=1,ans=0;
    while(left<=right)
    {
        ll mid=(left+right)/(ll)2;
        if(judge(mid)) left=mid+1;
        else right=mid-1;
    }
    printf("%lld\n",k*(left-1));
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll sum=0;
        scanf("%lld %lld",&n,&k);
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        if(sum<k)
            printf("0\n");
        else
            binary_Search(sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值