题面
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 个垃圾和一个机器人,每个垃圾有一个坐标
(xi,yi) 和一个重量 Wi ,机器人有一个最大载重 C
两点之间的移动距离是两点的曼哈顿距离,现在机器人要将这n 个垃圾全部捡掉,并且在捡第 i 个垃圾之前,要先把前i−1 个垃圾全部捡完,求最小移动距离
解法
单调队列优化 DP :
设 fi 表示解决前 i 个垃圾的最小移动距离,sdi 表示从1号垃圾依次移动到 i 号垃圾的移动距离,sumi 为 W 的前缀和,那么有:
fi=min(fi,fj+sdi+di−sdj+1+dj+1),sumi−sumj≤C
表示 j+1 到 i 号垃圾一次性捡走的代价
将式子中与j 有关的项拿出来: gj=fj−sdj+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;
}