2015多校联合第二场hdu5303Delicious Apples DP 类似最长山峰序列

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
 


题意:在圆环形的路上,某些地方xi有不定量的苹果ai,某人手里有最多能装k个苹果的篮子,取满还得回到原点 问:至少走多远

看到这个题,可能是贪心或是稍难的思维题?贪心的题解没看懂 dp明白了,特别特别像之前林大oj的周赛 987孙大神的面试 对于本题而言 所走的最短长度无非是顺时针+逆时针的

和的最小值,就这点特别像最长山峰序列有木有!反正就是顺着推一遍,逆着推一遍 最后找最小

#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
struct nod
{
    LL x,a;
    bool operator <(const nod &z) const
    {
        return x<z.x;
    }
}in[100005];//这里少写了个0 WA了一次
LL dp[2][100005];
LL l,n,k,sum;
LL x[100005],a[100005];
void init()
{
    memset(dp,0,sizeof(dp));
    sum=0;
}
void solve()
{
    dp[0][0]=dp[1][0]=0;
    LL num=1,pre;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<a[i];j++)
        {
            pre=num-k>0?num-k:0;
            dp[0][num]=dp[0][pre]+(2*x[i]>l?l:2*x[i]);
            num++;
        }
    }
    num=1;
    for(int i=n-1;i>=0;i--)
    {
        for(int j=0;j<a[i];j++)
        {
            pre=num-k>0?num-k:0;
            dp[1][num]=dp[1][pre]+(2*l-2*x[i]>l?l:2*l-2*x[i]);
            num++;
        }
    }
    LL res=0x3f3f3f3f3f3f3f3f;
    for(int i=0;i<=sum;i++)
        res=min(res,dp[0][i]+dp[1][sum-i]);
    printf("%I64d\n",res);
}
int main()
{
    freopen("cin.txt","r",stdin);
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        init();
        scanf("%I64d%I64d%I64d",&l,&n,&k);
        for(int i=0;i<n;i++) scanf("%I64d%I64d",&in[i].x,&in[i].a);
        sort(in,in+n);
        for(int i=0;i<n;i++)
        {
            x[i]=in[i].x;
            a[i]=in[i].a;
            sum+=a[i];
        }
        solve();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值