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

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

Author
XJZX
 

Source
2015 Multi-University Training Contest 2
 

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+贪心))