ZOJ 2018校赛 G Postman

版权声明:虽然我依旧蒟蒻,但请你尊重我 :D   ——陈杉菜 https://blog.csdn.net/qq_44702847/article/details/89302653

157 - The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) - G

Postman
Time Limit: 1 Second Memory Limit: 65536 KB

letters have just arrived at the post office positioned at , and the -th letter should be posted to position . 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 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 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 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 , indicating the number of test cases. For each test case:

The first line contains two integers and (), indicating the total number of letters and the capacity of the backpack.

The second line contains integers (), indicating the destination of each letter.

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

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 .

大声bb

就是送信,一共有m封,一次只能拿k封,拿了之后不能送回到邮局,求最短路径
就是排绝对值的序,氦,我最后才想到用两个数组分开存,不难想到所有的路都要走两遍,但是题目里又说,宝宝没必要回到邮局,在最后的时候,于是乎,要减去最长的那一次得到的结果才是最小的路径。听起来很容易,其实,也不是很难,,,菜是原罪,当时调了很久才打出来。还有就是k的值要注意一下。

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctype.h>
#include<vector>
#include<map>
#include<set>
#include<queue> 
#include<iomanip>
#include<list>
#include<fstream>
using namespace std;
typedef long long ll;
ll p[100010],f[100010],z[100010];
bool bmp(ll a,ll b){
	return a>b;
}
int main(){
	int t,n,k;
	ll ans=0;
	int f0,z0,i;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		memset(p,0,sizeof(p));
		memset(f,0,sizeof(f));
		memset(z,0,sizeof(z));
		ans=0;
		f0=1;
		z0=1;
		for(i=1;i<=n;i++){
			scanf("%lld",&p[i]);
			if(p[i]<=0){
				f[f0++]=-p[i];
			}
			if(p[i]>0)
				z[z0++]=p[i];
		}
		f0--;
		z0--;
		sort(f+1,f+f0+1,bmp);
		sort(z+1,z+z0+1,bmp);
		for(i=1;i<=f0;i+=k){
			ans+=f[i]*2;
		}
		if(i<=f0 && i>k) 
			ans+=f[i]*2;
		for(i=1;i<=z0;i+=k){
			ans+=z[i]*2;
		}
		if(i<=z0 && i>k) 
			ans+=z[i]*2;
		ans-=max(z[1],f[1]);
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44702847/article/details/89302653
ZOJ