LA3983 捡垃圾的机器人
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
1
10
4
1 2 3
1 0 3
3 1 4
3 1 4
Sample Output
14
一个机器人从(0,0)开始按照序号从小到大依次拣起n个坐标点上的垃圾(重量为ci),并回到原点。但是任何时候手上垃圾都不能超过重量C。任意两点距离为曼哈顿距离。求机器人行走的最短路程。
关键词:动态规划,单调队列优化
状态设计:数据量:n<=100 000,c<=100。二维dp状态空间不足,只能设计一维状态。dp[i]表示拣完第i个点垃圾回到原点的最短距离。
状态转移:枚举最后一次捡垃圾的起点。dp[i] = min{ dp[j] + dist2origin[j+1] + total_dist[i]-total_dist[j+1] + dist2origin[i] | 0<=j<=i-1 && sum_weight[i]-sum_weight[j]<=c }
优化:复杂度过高,需要优化。简化dp表达式,将仅含i的表达式提出来dp[i] = min{ dp[j]+dist2origin[j+1]-total_dist[j+1] | 0<=j<=i-1 && sum_weight[i]-sum_weight[j]<=c } + total_dist[i] + dist2origin[i]
令func(j)=dp[j]+dist2origin[j+1]-total_dist[j+1],进一步简化为dp[i]=min{ func(j) | 0<=j<=i-1 && sum_weight[i]-sum_weight[j]<=c} + total_dist[i] + dist2origin[i].
通过观察可以发现,对于任意的i,满足sum_weight[i]-sum_weight[j]<=c这个条件j总是一个连续区间。因为sum_weight[j]是递增的,因此sum_weight[i]-sum_weight[j]是递减的,较小的j若满足不等式,对于较大的j一定满足此式。
单调队列优化:更新并询问滑动区间的最大值/最小值
http://www.cnblogs.com/FuTaimeng/p/5422891.html
代码如下:
#include<iostream>
#include<cmath>
#define Size 100005
using namespace std; int d[Size];
int x[Size],y[Size];
int dist2origin[Size];
int total_weight[Size];
int total_dist[Size];
int q[Size];
int n,c; inline int func(int i){ return d[i]-total_dist[i+]+dist2origin[i+]; } int main(){
freopen("31.in","r",stdin); int T; cin>>T;
while(T--){
cin>>c>>n;
total_dist[]=total_weight[]=x[]=y[]=;
int w;
for(int i=;i<=n;i++){
cin>>x[i]>>y[i]>>w;
dist2origin[i]=abs(x[i])+abs(y[i]);
total_weight[i]=total_weight[i-]+w;
total_dist[i]=total_dist[i-]+abs(x[i]-x[i-])+abs(y[i]-y[i-]);
} int front=,rear=;
for(int i=;i<=n;i++){
//弹出队首元素直到从front到i的质量之和大于c或者队列为空
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(q[rear])>=func(i))rear--; q[++rear]=i;
} cout<<d[n]<<endl;
} fclose(stdin);
return ;
}
LA3983 捡垃圾的机器人的更多相关文章
- UVALive 3983 捡垃圾的机器人 DP
这个题目我最初的做法沿用树形DP的做法,设置一个 dp[i][0]表示机器人在i点不回去的最短路径,dp[i][1]表示机器人在i点回去的最短路径,规划方向为i-1向i转移,结果发现这个不能用树形的结 ...
- 单调队列优化dp(捡垃圾的机器人)
/************************************************************************* > File Name: a.cpp > ...
- PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...
- (1)RGB-D SLAM系列- 工具篇(硬件+关键技术)
/*************************************************************************************************** ...
- Confluence 6 在 Apache 或者系统级别阻止垃圾
如果一个垃圾发布机器人攻击你的 Confluence 站点,这些程序可能来自于同一个 IP 地址,或者是一个比较小范围的 IP 地址段.希望找到攻击者的 IP 地址,请参考 Apache access ...
- 五种WordPress防止垃圾评论方法-过滤垃圾评论提高WP运行效率
WordPress貌似和垃圾评论是一对“孪生兄弟”,无论在国内还是国外的空间主机上搭建的Wordpress博客,无论Wordpress有多少流量多么低的权重,垃圾评论都会自动找上门来,假如有好几天没有 ...
- 这货到底还是不是垃圾?【垃圾回收GC算法JVM篇四】
目录 1.判断对象是否存活的JVM两种计数算法 2.垃圾收集算法 3.垃圾回收算法小结 垃圾收集 Garbage Collection 通常被称为"GC", 在jvm 中,程序计数 ...
- 为Facebook messenger平台开发聊天机器人
介绍 在电子商务网上商店发明之前,我们总是有机会与销售代表或分销商在选择商品或服务时交谈.在进入数字世界后,这个领域变得沉默.这样对顾客方便吗?我认为不是.向销售代表或经销商询问他们想要的产品或服务是 ...
- uva 10599 - Robots(II) (dp | 记忆化搜索)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
随机推荐
- SmtpClient发送邮件
使用第三方SMTP服务器来发送邮件.如网易: SmtpClient sc = new SmtpClient("smtp.126.com"); sc.Credentials = ne ...
- 2、Hive UDF编程实例
Hive的UDF包括3种:UDF(User-Defined Function).UDAF(User-Defined Aggregate Function)和UDTF(User-Defined Tabl ...
- HTML第一讲
HTML标记区分 HTML即超文本标记语言(HtyperText Markup Language),其作用就是将编辑的内容在屏幕上显示.文件的后缀为.HTML. 在HTML中成对出现的叫做双标记(譬如 ...
- Nginx限制服务地址
今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案. 我们在使用的时候会遇到很多的恶 ...
- 示例文件下载demo
页面: 后台: package com.js.ai.modules.shwindow.util; import java.io.BufferedInputStream; import java.io. ...
- 线程之死锁、递归锁、信号量、事件Event 、定时器
1.死锁的现象 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相 ...
- Django组件—forms组件
forms组件: 校验字段功能: 针对一个实例:注册用户. 模型:models.py class UserInfo(models.Model): name=models.CharField(max_l ...
- 17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal
01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据, ...
- 使用Dottrace跟踪.net代码执行时间
当程序遇到性能问题,如IIs请求反应缓慢,.net客户端程序执行缓慢,如何分析是哪里出了问题?dottrace可以帮助.net程序跟踪出代码里每个方法的执行时间,清晰的看出是哪里执行时间过长,然后再分 ...
- sysbench基准测试(2)——oltp.lua测试
前面知道sysbench基准测试的主要步骤为:prepare(准备数据集)→ run(运行测试)→ cleanup(清除数据集) 这一节介绍oltp.lua测试. oltp基准测试模拟了一个简单的事物 ...