【暑假】[深入动态规划]UVAlive 3983 Robotruck
2015-08-22 11:38
417 查看
UVAlive 3983 Robotruck
题目:
Robotruck
Submit Status
Description
Problem C - Robotruck
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.
10
4
1 2 3
1 0 3
3 1 4
3 1 4
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
思路:
设d[i]为将前i个垃圾收完并放进垃圾桶的最小距离。同时定义total_dist[i]为从0点依次走过前i个垃圾到i的曼哈顿距离和,定义dist2origin[i]为i到0点的曼哈顿距离。
转移方程:
d[i]=min{d[j]-total_dist(j+1)+dist2origin(j+1) // j 满足w(j+1,i)<=c } +total_dist[i]+dist2origin[i] //代表一次将j+1..i的垃圾收回放到0点
单调队列优化:
设func(j)=d[j]-total_dist(j+1)+dist2origin(j+1) 则转移方程为:d[i]=min{func(j) // j 满足w(j+1,i)<=c } + ...
用单调队列维护一个满足w(q[front]+1,i)<=c的滑动窗口,单调队列中的值按照func的递增序排列。每次考虑一个新的i 都要适当移动窗口,使得满足单调队列的性质。维护的单调队列可以在O(1)的时间内返回min{func(j) // j 满足w(j+1,i)<=c}
具体操作如下:
完整代码:
题目:
Robotruck
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Problem C - Robotruck
Background
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.
Problem
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
One line containing one integer representing the minimum number of moves the robot must travel to deliver all the packages. Print a blank line between datasets.Sample Input
110
4
1 2 3
1 0 3
3 1 4
3 1 4
Sample Output
14-------------------------------------------------------------------------------------------------------------------------------------------------------------------
思路:
设d[i]为将前i个垃圾收完并放进垃圾桶的最小距离。同时定义total_dist[i]为从0点依次走过前i个垃圾到i的曼哈顿距离和,定义dist2origin[i]为i到0点的曼哈顿距离。
转移方程:
d[i]=min{d[j]-total_dist(j+1)+dist2origin(j+1) // j 满足w(j+1,i)<=c } +total_dist[i]+dist2origin[i] //代表一次将j+1..i的垃圾收回放到0点
单调队列优化:
设func(j)=d[j]-total_dist(j+1)+dist2origin(j+1) 则转移方程为:d[i]=min{func(j) // j 满足w(j+1,i)<=c } + ...
用单调队列维护一个满足w(q[front]+1,i)<=c的滑动窗口,单调队列中的值按照func的递增序排列。每次考虑一个新的i 都要适当移动窗口,使得满足单调队列的性质。维护的单调队列可以在O(1)的时间内返回min{func(j) // j 满足w(j+1,i)<=c}
具体操作如下:
int q[maxn]; //单调队列 front=rear=1; for(int i=0;i<=n;i++) { while(front<=rear && total_w[i]-total_w[q[front]]>c) front++; //维护队列中w(j+1,i)<=c d[i]=func(q[front]) + total_dist[i] + dist2origin[i]; //func(q[front])是队列中的最小值 while(front<=rear && func(i)<=func(q[rear])) rear--; //维护单调队列中的单调递增序 q[++rear]=i; }
完整代码:
#include<iostream> #include<algorithm> using namespace std; const int maxn = 100000 + 10; int x[maxn],y[maxn]; int total_dist[maxn],dist2origin[maxn],total_w[maxn]; int d[maxn]; inline int func(int j) { return d[j]-total_dist[j+1]+dist2origin[j+1]; } int main() { int T,n,c,w,front,rear; cin>>T; while(T--) { cin>>c>>n; total_w[0]=total_dist[0]=x[0]=y[0]=0; for(int i=1;i<=n;i++) { cin>>x[i]>>y[i]>>w; total_w[i]=total_w[i-1]+w; total_dist[i]=total_dist[i-1]+abs(x[i]-x[i-1])+abs(y[i]-y[i-1]); dist2origin[i]=abs(x[i])+abs(y[i]); } int q[maxn]; front=rear=1; for(int i=0;i<=n;i++) { while(front<=rear && total_w[i]-total_w[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; } cout<<d <<endl; if(T) cout<<endl; } }
相关文章推荐
- html基础知识
- Android程序设计:ExpandableListView或ListView的Item的子事件写法
- UISegmentControl
- Extjs单元格的样式设计
- Android中attr自定义属性,记录而已
- 针对Eclipse的maven Missing artifact com.microsoft.sqlserver:slqjdbc4:jar:4.0
- TestGN-Annotation
- 如何让nodejs同步操作
- 新网站如何做SEO?收录、排名So Easy
- Android之在Dialog中添加单选按钮【自定义Dialog】
- RTP 记录 log 该机制
- sql海量数据优化
- MySQL必知必会(Select)
- Spring Autowiring by Type
- 博客更新
- 信念之花
- 人生36条经验格言
- 单一职责原则(SRP)
- GCC在C语言中内嵌汇编 asm __volatile__ (2)
- Java GC 日志详解