(中等) CF 311B Cats Transport,斜率优化DP。

  Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1.

One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.

For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.

Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.

 

  队伍训练时做到的题目,好不容易推出了dp公式并且想到了斜率优化,结果犯了SB错误导致一直错。。。

  题目大致就是几只猫在一些地方,然后让人去收猫。。。饿还是看题吧。。。

  直接对于某个位置的猫的 t 减去到这个位置的时间就好,问题就转换成了有一些猫每个猫都有一个值,然后给P个人分别一个值,然后每个猫的找到比他大的最近的那个人的值,然后相减,累加每个猫的,让总和最小。。。饿,表达稍微比较烂。。。

  这里考虑到每个人的值一定是某只猫的值H,不然向下移动一点点可以更优。

  对每个猫的值进行排序,然后从左到右dp,

  dp[i][j]表示前i个猫,使用了j个人,的最小值。。。

  然后递推的话 dp[i][j]=min{ dp[x][j-1]+(i-x)H[i]-S[i]+S[x] };

  S表示H的前缀和。

  然后转换一下变成 min{ dp[x][j-1]+S[x]-xH[i] } + iH[i]-S[i];

  然后 Y[x]=dp[x][j-1] ,X[x]=x;

  然后就是经典斜率DP的问题了。。。

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年10月09日 星期五 18时45分52秒
// File Name     : B.cpp

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=100005;

int N,M,P;
long long DP1[MaxN],DP2[MaxN];
long long *dp1,*dp2;
long long H[MaxN];
long long S[MaxN];

long long X[MaxN],Y[MaxN],cou;

long long d[MaxN];

bool better(int a,int b,long long H)
{
    return (Y[a]-X[a]*H)<=(Y[b]-X[b]*H);
}

bool judge(long long x1,long long y1,long long x2,long long y2,long long x3,long long y3)
{
    return (y1-y2)*(x2-x3)<=(y2-y3)*(x1-x2);
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ios::sync_with_stdio(false);
    cin>>N>>M>>P;

    d[1]=0;
    for(int i=2;i<=N;++i)
    {
        cin>>d[i];
        d[i]+=d[i-1];
    }
    long long a,b;
    for(int i=1;i<=M;++i)
    {
        cin>>a>>b;
        H[i]=b-d[a];
    }

    dp1=DP1;
    dp2=DP2;
    N=M;
    sort(H+1,H+N+1);

    S[0]=0;
    for(int i=1;i<=N;++i)
    {
        S[i]=S[i-1]+H[i];
        dp1[i]=H[i]*i-S[i];
    }

    int p;
    long long TX,TY;
    P=min(P,N);

    for(int j=2;j<=P;++j)
    {
        cou=1;
        Y[0]=dp1[j-1]+S[j-1];
        X[0]=j-1;
        p=0;

        for(int i=j;i<=N;++i)
        {
            while(p<cou-1 && better(p+1,p,H[i])) ++p;
            dp2[i]=Y[p]-X[p]*H[i]-S[i]+i*H[i];

            TX=i;
            TY=dp1[i]+S[i];

            while(cou-1>p && judge(TX,TY,X[cou-1],Y[cou-1],X[cou-2],Y[cou-2])) --cou;
            X[cou]=TX;
            Y[cou++]=TY;
        }
        swap(dp1,dp2);
    }

    cout<<dp1[N]<<endl;

    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whywhy/p/4865252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
map pfn expected mapping type uncached-minus for [mem 0x7c11f000-0x7c11ffff], got write-back 这个问题怎么解决,这个会导致系统死机吗?PID: 500 TASK: ffff8800740d6dd0 CPU: 4 COMMAND: "mate-settings-d" #0 [ffff88024a6e7988] machine_kexec at ffffffff81059cdb #1 [ffff88024a6e79e8] __crash_kexec at ffffffff81105182 #2 [ffff88024a6e7ab8] crash_kexec at ffffffff81105270 #3 [ffff88024a6e7ad0] oops_end at ffffffff8168ed88 #4 [ffff88024a6e7af8] no_context at ffffffff8167e993 #5 [ffff88024a6e7b48] __bad_area_nosemaphore at ffffffff8167ea29 #6 [ffff88024a6e7b90] bad_area_nosemaphore at ffffffff8167eb93 #7 [ffff88024a6e7ba0] __do_page_fault at ffffffff81691b1e #8 [ffff88024a6e7c00] do_page_fault at ffffffff81691cc5 #9 [ffff88024a6e7c30] page_fault at ffffffff8168df88 [exception RIP: dev_set_drvdata+26] RIP: ffffffff8142c60a RSP: ffff88024a6e7ce8 RFLAGS: 00010206 RAX: 0000000900000000 RBX: ffff880258686098 RCX: 0000000180040001 RDX: ffff8801849e4000 RSI: 0000000000000000 RDI: ffff880258686098 RBP: ffff88024a6e7cf8 R8: ffff8801849e4000 R9: 0000000180040001 R10: 00000000849e6001 R11: ffffea0006127800 R12: ffff880239383398 R13: ffff880239383300 R14: ffff880061c29d08 R15: 0000000000000246 ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #10 [ffff88024a6e7d00] snd_usb_audio_free at ffffffffa059a587 [snd_usb_audio] #11 [ffff88024a6e7d28] snd_usb_audio_dev_free at ffffffffa059a5b2 [snd_usb_audio] #12 [ffff88024a6e7d38] __snd_device_free at ffffffffa02e2dc9 [snd] #13 [ffff88024a6e7d50] snd_device_free_all at ffffffffa02e311b [snd] #14 [ffff88024a6e7d70] release_card_device at ffffffffa02dd7de [snd] #15 [ffff88024a6e7d90] device_release at ffffffff814273f2 #16 [ffff88024a6e7db8] kobject_release at ffffffff8131a29e #17 [ffff88024a6e7de8] kobject_put at ffffffff8131a158 #18 [ffff88024a6e7e00] put_device at ffffffff81427717 #19 [ffff88024a6e7e10] snd_card_file_remove at ffffffffa02de1b4 [snd] #20 [ffff88024a6e7e40] snd_ctl_release at ffffffffa02df421 [snd] #21 [ffff88024a6e7e78] snd_disconnect_release at ffffffffa02ddafd [snd] #22 [ffff88024a6e7ea8] __fput at ffffffff811fff09 #23 [ffff88024a6e7ef0] ____fput at ffffffff812001be #24 [ffff88024a6e7f00] task_work_run at ffffffff810accc7 #25 [ffff88024a6e7f30] do_notify_resume at ffffffff8102ab22 #26 [ffff88024a6e7f50] int_signal at ffffffff8169677d gdb调试的bt,这个怎么判断是什么造成的死机
07-20

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值