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

HDU 5303(Delicious Apples- 环上折半dp+贪心)

时间:2015-08-16 19:55:27      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

Delicious Apples

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


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 x技术分享i技术分享技术分享, clockwise from position 0技术分享. There are a技术分享i技术分享技术分享 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,k10技术分享5技术分享,a技术分享i技术分享1,a技术分享1技术分享+a技术分享2技术分享+...+a技术分享n技术分享10技术分享5技术分享技术分享
1L10技术分享9技术分享技术分享
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 x技术分享i技术分享,a技术分享i技术分享技术分享.
 

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
 

Author
XJZX
 

Source
 

Recommend
wange2014
 

半环拆成1半,左边的尽可能左走,右边的右走,

现在考虑一下绕圈:

由于2次绕圈可以拆成左右各来回一次,一定不劣,

所以考虑1次绕圈,显然取k个苹果最优

剩下的苹果由dp贪心得到。

dp[i]=dp[i-k] +pos[i] //显然第i次是去pos[i],相当于尽量取远的,剩下的就是dp[i-k]


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
ll dpl[MAXN],dpr[MAXN],L;
int n,K,n1,n2;
ll al[MAXN],ar[MAXN];
void work(ll *dp,int n,ll *pos)
{
	sort(pos+1,pos+1+n);
	For(i,n) {
		if (i<=K) dp[i]=pos[i];
		else dp[i]=pos[i]+dp[i-K];
	}
} 
int main()
{
//	freopen("D.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int T;cin>>T;
	while(T--) {
		cin>>L>>n>>K;
		n1=n2=0;
		MEM(al) MEM(ar) MEM(dpl) MEM(dpr) 
 		For(i,n)
		{
			int xi,ai;
			scanf("%d%d",&xi,&ai);
			if (xi<=L/2) while (ai--) al[++n1]=xi;
			else while (ai--) ar[++n2]=L-xi;
		}
		work(dpl,n1,al);
		work(dpr,n2,ar);
		
		ll ans=(dpl[n1]+dpr[n2])*2;
		
		Rep(i,K+1) ans=min( 2LL*( dpl[max(n1-i,0)] + dpr[max(n2-(K-i),0)]  ) + L , ans  );
		cout<<ans<<endl;
	}
	return 0;
}


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

HDU 5303(Delicious Apples- 环上折半dp+贪心)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/47704097

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