题意:有一个机器人要捡n个垃圾到垃圾桶(0, 0)里,给出了机器人最多可以捡多重的垃圾c,然后给出了n个垃圾的位置和重量,机器人只能按给出的顺序捡垃圾,然后一起扔到垃圾桶里,问把所有垃圾清理完机器人行走的最短距离。
分析:《训练指南》P73。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int x[maxn], y[maxn];
int total_dist[maxn], total_weight[maxn], dist2origin[maxn];
int q[maxn], d[maxn];
int func(int i) {
return d[i] - total_dist[i+1] + dist2origin[i+1];
}
main() {
int T, c, n, w, front, rear;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &c, &n);
total_dist[0] = total_weight[0] = x[0] = y[0] = 0;
for(int i = 1; i <= n; i++) {
scanf("%d%d%d", &x[i], &y[i], &w);
dist2origin[i] = abs(x[i]) + abs(y[i]);
total_dist[i] = total_dist[i-1] + abs(x[i]-x[i-1]) + abs(y[i]-y[i-1]);
total_weight[i] = total_weight[i-1] + w;
}
front = rear = 1;
for (int i = 1; i <= n; i++) {
while (front <= rear && total_weight[i] - total_weight[q[front]] > c) front++;
d[i] = func(q[front]) + total_dist[i] + dist2origin[i];
while (front <= rear && func(i) <= func(q[rear])) rear--;
q[++rear] = i;
}
printf("%d\n", d[n]);
if(T > 0) printf("\n");
}
return 0;
}