C. Minimize Distance(思维,排序)

A total of nn depots are located on a number line. Depot ii lies at the point xixi for 1≤i≤n1≤i≤n.

You are a salesman with nn bags of goods, attempting to deliver one bag to each of the nn depots. You and the nn bags are initially at the origin 00. You can carry up to kk bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods.

Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤105001≤t≤10500). Description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105).

The second line of each test case contains nn integers x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−109≤xi≤109). It is possible that some depots share the same position.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots.

Example

input

Copy

4
5 1
1 2 3 4 5
9 3
-5 -10 -15 6 5 8 3 7 4
5 3
2 2 3 3 3
4 2
1000000000 1000000000 1000000000 1000000000

output

Copy

25
41
7
3000000000

Note

In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: 0→2→0→4→0→3→0→1→0→50→2→0→4→0→3→0→1→0→5, where each 00 means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of 2525 units. It must be noted that there are other sequences that give the same distance.

In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: 0→6→8→7→0→5→4→3→0→(−5)→(−10)→(−15)0→6→8→7→0→5→4→3→0→(−5)→(−10)→(−15), with distance 4141. It can be shown that 4141 is the optimal distance for this test case.

思路:

1,坐标相邻,距离才有可能最小,另外先到最远的地方,距离最小,因此从大到小计算.

2,分成正负,因为是对称的

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,m,n) for(int i=m;i<=n;++i)
#define pll pair<long,long>
#define vi vector<int>
#define si set<int>
 ; const int maxj =2e5+100,mod = 998244353 ;
void solve(){
    int n,k;//走的次数要少,先到最远的地方
    cin>>n>>k;
    vi a,b;
    rep(i,1,n)
    {
        int x;
        cin>>x;
        if(x >= 0 ) a.push_back(x);
        else b.push_back(-x);
    }
    int ca=a.size(),cb=b.size();
    sort(a.begin(),a.begin()+ca);
    sort(b.begin(),b.begin()+cb);
    
    int ans=0;
    for(int i=ca-1 ; i >= 0 ; i -= k){
        ans+=a[i]*2;
    }
    for(int i=cb-1;i>=0;i-=k){
        ans+=b[i]*2;
    }
    int mx=0;
    if(ca!=0)mx=max(a.back(),mx);
    if(cb!=0)mx=max(mx,b.back());
    ans-=mx;
    cout<<ans<<'\n';
}
int32_t main(){
    // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--) solve() ;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值