HDOJ 5303 Delicious Apples 枚举+DP


暴力枚举+DP

虽然是在环上,但最多只需要走一圈...

dp[0][i]表示从1...i从起点逆时针走取完i个的花费,有 dp[0][i]=dp[0][i-k]+dist[i]*2

dp[1][i]表示从i...n从起点顺时针走取完n-i+1个的花费 dp[1][i]=dp[1][i+k]+(L-dist[i])*2

枚举哪些点顺时针哪些点逆时针: ans=min(ans,dp[0][i]+dp[1][i+1]);

然后枚举从哪个点开始走一圈:ans=min(ans,dp[0][max(0,i-K)]+dp[1][i+1]+L);


Delicious Apples

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


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.
 

Sample Input
  
  
2 10 3 2 2 2 8 2 5 1 10 4 1 2 2 8 2 5 1 0 10000
 

Sample Output
  
  
18 26
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5309  5307  5306  5304  5302 
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年07月24日 星期五 16时13分36秒
File Name     :HDOJ5303.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;
const int maxn=100100;

struct Apple
{
	int pos,val;
	bool operator<(const Apple& apple) const
	{
		return pos<apple.pos;
	}
}apple[maxn];

int L,m,K,n;
LL dist[maxn];
LL dp[2][maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		memset(dist,0,sizeof(dist));
		memset(dp,0,sizeof(dp));

		scanf("%d%d%d",&L,&m,&K);
		for(int i=0,x,y;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			apple[i]=(Apple){x,y};
		}

		n=1;
		sort(apple,apple+m);
		for(int i=0,x,y;i<m;i++)
		{
			x=apple[i].pos;
			y=apple[i].val;
			if(x==0||x==L) continue;
			while(y--) dist[n++]=x;
		}

		for(int i=1;i<n;i++)
		{
			dp[0][i]=dp[0][max(0,i-K)]+dist[i]*2;
		}

		for(int i=n-1;i>0;i--)
		{
			dp[1][i]=dp[1][min(n+1,i+K)]+(L-dist[i])*2;
		}

		LL ans=min(dp[0][n-1],dp[1][1]);

		for(int i=1;i<n;i++)
		{
			ans=min(ans,dp[0][i]+dp[1][i+1]);
			ans=min(ans,dp[0][max(0,i-K)]+dp[1][i+1]+L);
		}

		cout<<ans<<endl;
	}
    
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值