hdu 5303

Delicious Apples


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
 

 題目大意:給定一個環形道路長度爲L,以及環形道路下標爲0處爲起始點,在環形道路上距
      離起始點Xi位置種植一顆蘋果樹,該樹有a個蘋果,籃子的最大容量爲K,那麼求摘
      完全部蘋果所需的最短距離。

思路:最後一籃想了多種可能性,結果太亂不知道怎麼解決了。。。
   將環形圈分成兩個半圓,在每個半圓裏儘可能的裝蘋果,但是,最後一籃蘋果可能是繞了整個
   環形圈走的,所以這裏需要特殊處理一下。
   剛開始的思路就卡在最後一籃蘋果上了,看了別人的代碼,如果自己寫可能會寫的很複雜,
   把每個蘋果都納入數組,求出從兩個方向到達原點最近的距離,然後在最後一個籃子上進行枚舉。
   很精妙的思路和代碼。
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define M 100005
int Left[M],Right[M],numl,numr;
int n,k,l;
long long s_l[M],s_r[M],ans;
void slove()
{
    for(int i=1;i<=numl;i++){
        if(i<=k)
            s_l[i]=Left[i];
        else
            s_l[i]=Left[i]+s_l[i-k];
    }
    for(int i=1;i<=numr;i++){
        if(i<=k)
            s_r[i]=Right[i];
        else
            s_r[i]=Right[i]+s_r[i-k];
    }
    ans=(s_r[numr]+s_l[numl])*2;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&l,&n,&k);
        memset(s_l,0,sizeof(s_l));
        memset(s_r,0,sizeof(s_r));
        numl=numr=0;
        for(int i=1;i<=n;i++){
            int len,num;
            scanf("%d%d",&len,&num);
            for(int j=1;j<=num;j++){
                if(len*2<l)
                    Left[++numl]=len;
                else
                    Right[++numr]=l-len;
            }
        }
        sort(Left+1,Left+1+numl);
        sort(Right+1,Right+1+numr);
        slove();

        for(int i=0;i<=k;i++){
            long long ll = 2*(s_l[numl-i]+s_r[max(0,numr-k+i)]);
            ans=min(ans,ll+l);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章