hdu 5303 Delicious Apples 2015多校联合训练赛2 dp+枚举

Delicious Apples

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


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
 

Source



题意:

有一个圈,圈上不同位置有一定数量的苹果,苹果与起点有个距离。初始位置在0点。有个篮子,每次可以装K个苹果。问,要把所有的苹果都用篮子装到起点,至少要走多远的距离。


分析:

苹果总数 <= 100000. 那么按顺序排序每个苹果。

按顺时针,和逆时针分别处理。

dp[0][i]表示只能顺时针向前走,逆时针回到起点。把顺时针的前i个苹果带回原点的最小路程

            那么dp[0][i] = dp[0][i-K] + 2*dist[i]   -----------dist[i]表示第i个苹果顺时针距离起点的距离

同理逆时针的转移方程为

           dp[1][i] = dp[1][i+K] + 2*(L-dist[i])

然后枚举第i个苹果是分界线,i个苹果和之前的苹果是顺时针取得,i+1和之后的苹果是逆时针取得

         那么ans = min(ans,dp[0][i]+dp[1][i+1])

但是存在这样的情况,取走第i个苹果的时候,不原路放回,二是直接向前走到终点。

       那么ans = min(ans,dp[0][i-K]+dp[1][i+1] + L)

最后ans就是答案

  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100007
struct Point{
    int x;
    int num;
};
Point p[100007];
Point px[maxn];
int cmp(Point a,Point b){
    return a.x < b.x;
}
ll dp[2][maxn];
int main(){
    int t,L,n,K;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&L,&n,&K);
        int cnt = 1,x,y;
        for(int i = 0;i < n; i++){
            scanf("%d%d",&px[i].x,&px[i].num);
        }
        sort(px,px+n,cmp);
        for(int i = 0;i < n; i++){
            while(px[i].num--){
                p[cnt++].x = px[i].x;
            }
        }
        memset(dp[0],0,sizeof(ll)*(cnt+3));
        memset(dp[1],0,sizeof(ll)*(cnt+3));
        for(int i = 1;i < cnt; i++){
            dp[0][i] = dp[0][max(0,i-K)] + p[i].x * 2;
        }
        for(int i = cnt-1;i > 0; i--){
            dp[1][i] = dp[1][min(cnt,i+K)] + (L - p[i].x) * 2;
        }
        ll ans = min(dp[0][cnt-1],dp[1][1]);
        for(int i = 1;i < cnt; 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);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值