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 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?

1≤n,k≤105,ai≥1,a1+a2+...+an≤105

1≤L≤109

0≤x[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

2015 Multi-University Training Contest 2

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;
}

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

时间: 2024-08-01 09:43:23

HDOJ 5303 Delicious Apples 枚举+DP的相关文章

HDU 5303 Delicious Apples (贪心 枚举 好题)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 199    Accepted Submission(s): 54 Problem Description There are n apple trees planted along a cyclic road, which is L metres l

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 95 Problem Description There are  apple trees planted along a cyclic road, which is  metres lon

2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1057    Accepted Submission(s): 354 Problem Description There are n apple trees planted along a cyclic road, which is L metres

hdu 5303 Delicious Apples(dp)

题意:一个长为L的圈种上n颗树,每棵树的坐标为xi,结了ai个苹果,用大小为k的篮子把所有苹果装回来,问最少走多少路 解:被神奇的dp教做人了(其实我比较水,本来以为左边贪心一下,右边贪心一下在最后转一圈就搞定的水题=-=) #include <stdio.h> #include <string.h> #include <algorithm> #define ll __int64 #define MIN(a,b) ((a)<(b)?(a):(b)) const i

[多校2015.02.1004 dp] hdu 5303 Delicious Apples

题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针取i个苹果的最短距离. dp[1][i]代表逆时针取i个苹果的最短距离. 那么设苹果的总是为sum 那么ans=dp[0][i]+dp[sum-i]  (0<=i<=sum) 代码: #include"stdio.h" #include"algorithm"

HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 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,

多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: 首先我们能够(ˇ?ˇ) 想-,假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个圈劈开.那么对于每一个区间由于苹果数非常少,所以能够利用belong[x]数组记录每一个苹果所在的苹果树位置,然后将苹果依照所在的位置排序,那么也就是我们知道每次拿k个苹果的代价是苹果所在的最远

hdu 5303 Delicious Apples(背包)

题意:一个环长度为L,上面有n棵树,篮子一次可装K个苹果: 给出每棵树的位置和树上的苹果数,求将所有苹果运回原点的最少的总距离: 思路:将环分为两半考虑,且若有绕环一圈的情况也只能有一次: 以单个苹果为对象进行处理: 考虑不绕圈的情况:每个半圈优先取最远的苹果:sum[i]表示取第i个苹果是的花费(距离): 考虑绕圈的情况:枚举两个半圈共剩下k个苹果的情况,最后绕一圈: 不绕圈和绕圈中的最小值即为所求值: #include<cstdio> #include<cstring> #in

2015 Multi-University Training Contest 2 1004 Delicious Apples(DP)

题目链接 题意:长度为l 的环,有n棵果树,背包容量为k,告诉你k棵苹果树的id,以及每棵树上结的果子数,背包一旦装满要返回起点(id==0) 清空,问你至少走多少路,能摘完所有的苹果. 思路: 因为是环形,所以其实离起点最远的点应该是l / 2: 两种摘苹果的方式,一种从上半圈开始走,用dp[0][i]记录: 另外一种,从下半圈开始走,用dp[1][i]记录: allv 记录苹果总数,那么只要找出最小的 dp[0][i] + dp[1][all-i]就好啦! 代码如下: #include<cs