【UVA1169】Robotruck

14 篇文章 0 订阅

题面

  This problem is about a robotic truck that distributes mail packages to several locations in a factory.
  The robot sits at the end of a conveyer at the mail office and waits for packages to be loaded into its cargo area. The robot has a maximum load capacity, which means that it may have to perform several round trips to complete its task. Provided that the maximum capacity is not exceeded, the robot can stop the conveyer at any time and start a round trip distributing the already collected packages. The packages must be delivered in the incoming order.
  The distance of a round trip is computed in a grid by measuring the number of robot moves from the mail office, at location (0,0), to the location of delivery of the first package, the number of moves between package delivery locations, until the last package, and then the number of moves from the last location back to the mail office. The robot moves a cell at a time either horizontally or vertically in the factory plant grid. For example, consider four packages, to be delivered at the locations (1,2), (1,0), (3,1), and (3,1). By dividing these packages into two round trips of two packages each, the number of moves in the first trip is 3+2+1=6, and 4+0+4=8 in the second trip. Notice that the two last packages are delivered at the same location and thus the number of moves between them is 0.
  Given a sequence of packages, compute the minimum distance the robot must travel to deliver all packages.

题面

  有 n 个垃圾和一个机器人,每个垃圾有一个坐标xiyi和一个重量 Wi ,机器人有一个最大载重 C
  两点之间的移动距离是两点的曼哈顿距离,现在机器人要将这n个垃圾全部捡掉,并且在捡第 i 个垃圾之前,要先把前i1个垃圾全部捡完,求最小移动距离

解法

单调队列优化 DP
  设 fi 表示解决前 i 个垃圾的最小移动距离,sdi表示从1号垃圾依次移动到 i 号垃圾的移动距离,sumi W 的前缀和,那么有:
  

fi=minfifj+sdi+disdj+1+dj+1sumisumjC

  表示 j+1 i 号垃圾一次性捡走的代价
  将式子中与j有关的项拿出来: gj=fjsdj+1+dj+1 ,我们的目的就是找到一个最小的 gj ,而决策点肯定是单调的,所以可以将 g 丢入一个单调队列,然后就可以做到logn查找到最小的 g

复杂度

O(nlogn

代码

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define Lint long long int
using namespace std;
const int INF=0x3f3f3f3f;
const int N=100010;
struct node
{
    int x,y;
}p[N];
int q[N],head,tail;
int d[N],w[N],f[N];
int sum[N],sd[N];
int T,n,c;
int F(int x)   { return f[x]-sd[x+1]+d[x+1] ; }
int main()
{
    scanf("%d",&T);
    while( T-- )
    {
        scanf("%d%d",&c,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&w[i]);
            sum[i]=sum[i-1]+w[i];
            d[i]=p[i].x+p[i].y;
            sd[i]=sd[i-1]+abs(p[i].x-p[i-1].x)+abs(p[i].y-p[i-1].y);
        }
        head=tail=1;
        for(int i=1;i<=n;i++)  
        {  
            while( head<=tail && sum[i]-sum[q[head]]>c )   head++;
            f[i]=F( q[head] )+d[i]+sd[i];  
            while( head<=tail && F( i )<=F( q[tail] ) )   tail--;
            q[++tail]=i;
        }
        printf("%d\n",f[n]);
        if( T )   printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值