ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces 1250J The Parade

2020-12-17 19:04:39  阅读:372  来源: 互联网

标签:Parade ll each number Codeforces soldiers test 1250J parade


Codeforces 1250J 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

二分,题意是问一共有多少士兵可以参加游行,又要求必须要 k k k 行,那么我们可以二分一行的最大人数,因为要求每一行的人的高度差不超过 1 1 1,我们就直接贪心取,按高度从低到高取,取满 k k k 行为止,AC代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e4+5;
int t,n;
ll a[N],k,b[N];
bool check(ll x){
    ll u=k;
    for(int i=1;i<=n;i++){
        b[i]=a[i];
    }
    for(int i=1;i<=n;i++){//贪心取人
        ll cnt=b[i]/x;
        b[i]=b[i]%x;
        if(u>=cnt) u-=cnt;
        else return 1;
        if(b[i+1]+b[i]>=x){
            b[i+1]-=(x-b[i]);
            b[i]=0;
            u--;
        }
        if(u<=0) return 1;
    }
    return (u<=0);
}

int main()
{
    cin>>t;
    while(t--){
        memset(b,0,sizeof(b));
        cin>>n>>k;
        ll sum=0;
        for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
        ll l=1,r=sum/k+1;
        while(l<=r){
            ll mid=l+r>>1;
            if(check(mid)) l=mid+1;
            else r=mid-1;
        }
        cout<<r*k<<endl;
    }
    return 0;
}

标签:Parade,ll,each,number,Codeforces,soldiers,test,1250J,parade
来源: https://blog.csdn.net/qq_43765333/article/details/111319226

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有