3983 Robotruck
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 thefactory 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.
Input
Input consists of multiple test cases. The first line of the input contains the number of test cases.There is a blank line before each dataset.The input for each dataset consists of a line containing one positive integer C, not greater then 100,indicating the maximum capacity of the robot, a line containing one positive integer N, not greater than 100,000, which is the number of packages to be loaded from the conveyer. Next, there are N lines containing, for each package, two non-negative integers to indicate its delivery location in the grid, and a positive integer to indicate its weight. The weight of the packages is always smaller than the robot’s maximum load capacity. The order of the input is the order of appearance in the conveyer.
Output
For each test case, print one line containing one integer representing the minimum number of movesthe robot must travel to deliver all the packages.Print a blank line between datasets.
Sample Input
1
10
4
1 2 3
1 0 3
3 1 4
3 1 4
Sample Output
14
f[i]为将前i个垃圾清理完的最小距离
f[i]=min{f[j]+dis0[j+1]+dis[j+1,i],dis0[i]}
这里的i是相对为常量的,因此求的是有关j的最小值
注意到满足w(j+1,i)<=c的所有j形成一个区间,而且随着i的增大这个区间会向右移动,因此我们就用滑动窗口模型维护区间最小值就可以了
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<deque>
using namespace std;
const int maxn=100005;
inline void _read(int &x){
char t=getchar();bool sign=true;
while(t<'0'||t>'9')
{if(t=='-')sign=false;t=getchar();}
for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';
if(!sign)x=-x;
}
int f[maxn],x[maxn],y[maxn],t;
int sumw[maxn],dis0[maxn],sumd[maxn];
int fun(int x){return f[x]-sumd[x+1]+dis0[x+1];}//fun即为要维护的最小值
int main(){
_read(t);
while(t--){
int i,w,c,n;
_read(c);_read(n);
for(i=1;i<=n;i++){
_read(x[i]);_read(y[i]);_read(w);
sumw[i]=sumw[i-1]+w;
dis0[i]=abs(x[i])+abs(y[i]);
sumd[i]=sumd[i-1]+abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
}
deque<int>q;
q.push_back(0);
for(i=1;i<=n;i++){
while(q.size()&&sumw[i]-sumw[q.front()]>c)q.pop_front();
if(q.size())f[i]=fun(q.front())+sumd[i]+dis0[i];
while(q.size()&&fun(i)<=fun(q.back()))q.pop_back();
q.push_back(i);
}
printf("%d\n",f[n]);
if(t>0)putchar(10);
}
}