【UVA1169】Robotruck

来源:互联网 发布:软件测试毕业总结 编辑:程序博客网 时间:2024/05/22 20:53

题面

  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号垃圾的移动距离,sumiW的前缀和,那么有:
  

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

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

复杂度

O(nlogn

代码

#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#define Lint long long intusing 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;}
原创粉丝点击