hdu5303 Delicious Apples

2 篇文章 0 订阅

Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 436    Accepted Submission(s): 133


Problem Description
There are  n  apple trees planted along a cyclic road, which is  L  metres long. Your storehouse is built at position  0  on that cyclic road.
The  i th tree is planted at position  xi , clockwise from position  0 . There are  ai  delicious apple(s) on the  i th tree.

You only have a basket which can contain at most  K  apple(s). You are to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled?

1n,k105,ai1,a1+a2+...+an105
1L109
0x[i]L

There are less than 20 huge testcases, and less than 500 small testcases.
 

Input
First line:  t , the number of testcases.
Then  t  testcases follow. In each testcase:
First line contains three integers,  L,n,K .
Next  n  lines, each line contains  xi,ai .
 

Output
Output total distance in a line for each testcase.


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long dp[100001],dp1[100001],a[100001];
int main(){
	int T,n,k,x,y;
	long long l;
	scanf("%d",&T);
	while(T--){
		scanf("%I64d%d%d",&l,&n,&k);
		int m=0;
		for(int i=0;i<n;i++){
			scanf("%d%d",&x,&y);
			while(y){
				y--,m++;
				a[m]=x;
			}
		}
		sort(a+1,a+m+1);
		dp[0]=0;
		dp1[m+1]=0;
		for(int i=1;i<=m;i++)
			if(i-k<0) dp[i]=min(a[i]*2,l);
				else dp[i]=dp[i-k]+min(a[i]*2,l);
		for(int i=m;i>0;i--)
			if(i+k>m) dp1[i]=min((l-a[i])*2,l);
				else dp1[i]=dp1[i+k]+min((l-a[i])*2,l);
		long long ans=dp[0]+dp1[1];
		for(int i=1;i<=m;i++)
			ans=min(ans,dp[i]+dp1[i+1]);
		printf("%I64d\n",ans);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值