码迷,mamicode.com
首页 > 移动开发 > 详细

HDOJ 5303 Delicious Apples 枚举+DP

时间:2015-07-24 18:14:51      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:


枚举+DP

虽然在环上,但最多绕环走一次

dp[0][i]前i个点全部逆时针取完,dp[1][i]从i到n顺时针取完

然后枚举中间分界线,和从哪一个点开始转一圈


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 ith tree is planted at position xi, clockwise from position 0. There are ai delicious apple(s) on the ith 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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 5303 Delicious Apples 枚举+DP

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47044743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!