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 <= 100000), indicating the total number of letters and the capacity of the backpack.

The second line contains N integers a1,a2,...,aN (-1000000000 <= ai <= 1000000000), indicating the destination of each letter.

It’s guaranteed that the sum of N over all test cases will not exceed 1000000.

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 x = 0.

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 x = 3, then to x = -1, then to the post office), then deliver the 2nd, the 4th and the 5th letter (go to x = -2, then to x = -4, then to x = -5), and ends his delivery at x = -5.

题意:在一个数轴上,原点是邮局,每次到邮局可以拿不超过k封信,给出需要送信的坐标,问最少要跑多远

题解:到原点我们的信就加满了,正负半轴分开讨论
每一次送的k封信一定是连续的,那我们就把这些点每k个分成一段,那么每一段的代价都是从原点到这一段最远那个点的距离*2,由于最后一次只去无回,所以将到原点距离最远的减掉一个(正负半轴只能减一个!!!)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
LL t,n,k,mx,ai,ni,sum,a[N],act[N],neg[N];//long long 类型
bool cmp(LL a,LL b){
	return a > b;
}
int main(){
	ios :: sync_with_stdio(false);
	cin.tie(0);
	while(cin >> t){
		while(t--){
			cin >> n >> k;
			ai = ni = mx = sum = 0;
			for(int i = 0;i < n;i++) cin >> a[i],mx = max(mx,abs(a[i]));
			for(int i = 0;i < n;i++){
				if(a[i] < 0) neg[ni++] = -a[i];
				else act[ai++] = a[i];
			}
			sort(act,act + ai,cmp);
			sort(neg,neg + ni,cmp);
			for(int i = 0;i < ai;i += k){
				sum += act[i] * 2;
			}
			for(int i = 0;i < ni;i += k){
				sum += neg[i] * 2;
			}
			cout << sum - mx << endl;
		}			
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值