The Parade CodeForces - 1250J(二分+dfs)

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≤1012) — 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≤1012), 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:

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);
all soldiers can march in the same row;
33 soldiers with height 1 in each of 3 rows;
all soldiers can march in the same row;
all soldiers with height 2 and 3 can march in the same row.
思路:这是一个单调函数,具有单调性,因此我们可以二分每一行的人数,判断是否符合条件。在判断的时候,我们贪心的去想,前面的尽量用,以免后面不够的情况。在判断的时候,我是用的dfs,有一点要注意,如果当前值可以满足当前行的人数,那么我们就直接算出这一标号的人可以满足多少行,否则会RE的,因为K太大了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e4+100;
ll a[maxx];
int n;
ll k,sum;

inline void dfs(ll &row,int &flag,int pos,ll x)
{
	if(flag==0) return ;
	if(row>=k+1) return ;
	if(pos==n+1)
	{
		flag=0;
		return ;
	}
	if(a[pos]>x) 
	{
		ll t=a[pos]/x;
		a[pos]%=x;
		dfs(row+=t,flag,pos,x);//这里要注意
		a[pos]=t*x+a[pos];//这个操作类似于还原数组,这样就不用每一次都遍历数组还原了。
	}
	else if(a[pos]==x) dfs(row+=1,flag,pos+1,x);
	else
	{
		if(a[pos]+a[pos+1]<x)
		{
			dfs(row,flag,pos+1,x);
			return ;
		}
		a[pos+1]-=(x-a[pos]);
		dfs(row+=1,flag,pos+1,x);
		a[pos+1]+=(x-a[pos]);
	}
}
inline int check(ll x)
{
	if(sum<x*k) return 0;
	int flag=1;ll row=1;
	dfs(row,flag,1,x);
	return row>=k+1&&flag;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		sum=0;
		scanf("%d%lld",&n,&k);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum+=a[i];
		a[n+1]=0;
		ll l=1,r=1e16,mid,ans=0;
		while(l<=r)
		{
			mid=l+r>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		printf("%lld\n",ans*k);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Type-C Cable是一种符合Universal Serial Bus Type-C Cable and Connector Specification R2.0标准的USB-C数据线。\[1\]根据引用\[2\]的描述,由于非公开协议的限制,Intel TBT Gen3开始已经可以应用在USB-C Port上,所以对于TBT的相关Cable应该也会有兼容。而根据引用\[3\]的描述,谱瑞(Parade)收购了睿思科技(Fresco Logic),这可能意味着他们在USB这块有一定的技术积累。因此,Type-C Cable可能具备兼容TBT和USB 3.0的功能。 #### 引用[.reference_title] - *1* [【硬件】【USB】【Type-C】](https://blog.csdn.net/syjie19900426/article/details/109054926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Usb Type-C 1.2 Usb Type-C Cable](https://blog.csdn.net/Strider_kong/article/details/126056566)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [PS188——谱瑞(Parade)推动的 Type-C扩展坞红海战略](https://blog.csdn.net/Type_C_Ken/article/details/124349798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值