Postman 模拟

N letters have just arrived at the post office positioned at x=0, and the i -th letter should be posted to position x=ai . BaoBao, our beloved postman, will start his work from the post office and deliver all these letters by himself.

Unfortunately, BaoBao’s backpack can only hold at most k letters each time (which means that if he wants to deliver some letter not in his backpack, he will have to go back to the post office and fetch it), so he may not be able to deliver all n letters in one go. Please note that BaoBao cannot temporarily drop a letter outside the post office and pick it back afterward.

What’s the minimum distance BaoBao has to travel to deliver all N letters?

It’s NOT necessary that BaoBao ends his delivery in the post office.

Input
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers N and K (1<=K<=N<10^5), indicating the total number of letters and the capacity of the backpack.

The second line contains N integers ai (10^-9<=ai<= 10 ^9), indicating the destination of each letter.

It’s guaranteed that the sum of n over all test cases will not exceed 10^6.

Output
For each test case output one line containing one integer, indicating the minimum distance BaoBao has to travel to deliver all the letters, starting from the post office at .

Sample Input
2
5 3
-1 -2 3 -4 -5
6 3
1 0 -2 -1 1 2
Sample Output
13
6
Hint
For the first sample test case, BaoBao can first deliver the 1st and the 3rd letter (go to , then to , then to the post office), then deliver the 2nd, the 4th and the 5th letter (go to , then to , then to ), and ends his delivery at .

题目意思是说一个邮递员去送信,n表示要送的信的数目,k表示一次最多携带的信件数目,问送完信最少要走多远的距离,这里的送完信是说送完最后一个,这一次就不需要计算回邮局的距离了。输入的距离正数表示目的地在邮局右侧,负数是在左侧。

大佬学弟的办法,一次送满K封信是基本的思想,先分左边右边统计各有多少个信要送,假设左边有L个信件要送,那么从L%k往后,每次送K个,这是因为不满K的那一部分是一定要送的,无非区别在于离邮局距离的远近,同样都是要去到这一部分里面最远的那一个位置,去近的肯定要比去远的距离近,所以选择最靠近邮局的那几个,让这一部分不满K个送信,那么剩下的部分都是可以一次送K个那样送完,除了最远的那一个,最远的那一个要左右考虑,选取左右最远距离里面更大的那一个,让这一个为最后一次送的,那么就可以省下回来的距离,从而达到距离最小。

AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100010],b[100010];
int main(){
	int n,k,T,i,x,cnta,cntb;
	a[0]=0;b[0]=0;
	cin>>T;
	while(T--){
		cnta=1;cntb=1;
		scanf("%d%d",&n,&k);
		for(i=0;i<n;i++){ 
			scanf("%d",&x);
			if(x>0){
				a[cnta]=x;
				cnta++;
			}//分别统计左边和右边各有多少个 
			else if(x<0){
				b[cntb]=-x;
				cntb++;
			}
		}
		
		cnta--;
		cntb--;
		
		sort(a+1,a+cnta+1);
		sort(b+1,b+cntb+1);
		
		//最靠近邮局的几个即使不满足K个也单独送,最近的几个虽然一次送不满,但是距离近,相比于远处的
		//应该让远处的送满,近处的送不满。
		 
		long long suma=0;
		for(i=cnta%k;i<cnta;i=i+k)//左边的每K个送一次 
			suma+=2*a[i];	
		suma+=a[cnta];//最后一趟去了不回来 
		
		long long sumb=0;
		for(i=cntb%k;i<cntb;i=i+k)//右边的每K个送一次 
			sumb+=2*b[i];
		sumb+=b[cntb];//最后一趟去了不回来 
		
		long long sum=0;
		
		if(a[cnta]>b[cntb])//两边选不回来的那一趟最小的,让这一趟回来 
			sum=suma+sumb+b[cntb];
		else
			sum=suma+sumb+a[cnta];
			
		printf("%lld\n",sum);
	}
	return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ayakanoinu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值