UVaLive 3983 Robotruck (DP + 单调队列)
题意:有n个垃圾,第i个垃圾坐标为(xi,yi),重量为wi,有一个机器人,要按照编号从小到大的顺序剑气所有的垃圾兵扔进垃圾桶,垃圾桶在原点,
每次总重量不能超过C,两点间距离为曼哈顿距离,求出最短的距离和。
析:第一反应想到的状态是有个数和重量,一看,时间复杂度受不了,只能改。dp[i] 表示从原点出发倒掉前 i 个垃圾,并放到垃圾桶所要的最短距离。
dp[i] = min{dp[j] + dist(j+1, i) + disttoorigin(i) + disttoorigin(j+1)} j <= i w(j+i, i) <= C 其中dist(j+1, i) 表示第 j+1 个垃圾到第 i 个
垃圾的距离,disttoorigin(i) 表示第 i 个垃圾到原点的距离,w(i, j) 表示第 i-j个垃圾的总重量。 dist disttoorigin 都可以预处理出来。
dist(j, i) = totaldist(i) - totaldist(j)。dp[i] = min{fun[j] |w(j+1, i) <= C} + totaldist(i) + disttoorigin(i)。
fun[j] = dp[j] - totaldist(j+1) + disttoorigin(j+1),我们只要维护fun[i]最小就好了,所以就要维护一个单调队列。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int x[maxn], y[maxn], w[maxn];
int sum[maxn], sumw[maxn];
int q[maxn], dist[maxn];
int dp[maxn]; inline int func(int i){ return dp[i] - sum[i+1] + dist[i+1]; } int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &m, &n);
x[0] = y[0] = w[0] = sum[0] = sumw[0] = 0;
for(int i = 1; i <= n; ++i){
scanf("%d %d %d", x+i, y+i, w+i);
dist[i] = abs(x[i]) + abs(y[i]);
sum[i] = sum[i-1] + abs(x[i] - x[i-1]) + abs(y[i] - y[i-1]);
sumw[i] = sumw[i-1] + w[i];
}
int front = 1, rear = 1;
q[1] = 0;
for(int i = 1; i <= n; ++i){
while(front <= rear && sumw[i] - sumw[q[front]] > m) ++front;
dp[i] = func(q[front]) + dist[i] + sum[i];
while(front <= rear && func(i) <= func(q[rear])) --rear;
q[++rear] = i;
}
printf("%d\n", dp[n]);
if(T) puts("");
}
return 0;
}
UVaLive 3983 Robotruck (DP + 单调队列)的更多相关文章
- UVALive 3983 Robotruck (单调队列,dp)
如果状态定义为序号和重量的话,决策就是下一个垃圾捡或者不减,但是状态数太多了. 如果只定义序号作为状态的话,决策就变成从前面的某个j一直捡到i才送回垃圾. 这就变成了一个区间选最小值的问题,用单调队列 ...
- 【暑假】[深入动态规划]UVAlive 3983 Robotruck
UVAlive 3983 Robotruck 题目: Robotruck Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Descripti ...
- 习题:烽火传递(DP+单调队列)
烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...
- (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列
Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...
- 3622 假期(DP+单调队列优化)
3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...
- bzoj2500: 幸福的道路(树形dp+单调队列)
好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...
- URAL 1427. SMS(DP+单调队列)
题目链接 我用的比较传统的办法...单调队列优化了一下,写的有点搓,不管怎样过了...两个单调队列,存两个东西,预处理一个标记数组存... #include <iostream> #inc ...
随机推荐
- 【Spark Core】TaskScheduler源代码与任务提交原理浅析2
引言 上一节<TaskScheduler源代码与任务提交原理浅析1>介绍了TaskScheduler的创建过程,在这一节中,我将承接<Stage生成和Stage源代码浅析>中的 ...
- kubernetes之常见故障排除(一)
系列目录 由由种种原因,在安装或者使用kubernetes的过程中,可能会遇到各种各样的问题.本篇按照官网的介绍罗列出一些常见的故障,以帮助快速解决一些常见的错误. 安装赛程中出现ebtables o ...
- nanoporetech/nanonet
nanoporetech/nanonet CodeIssues 7Pull requests 0Projects 0Wiki Insights First generation RNN baseca ...
- Vuex demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- MongoDB的一些操作技巧
去年三月底入职上海的一家互联网公司,由于项目使用的是MongoDB数据库所以有机会接触了MongoDB.在项目的开发过程中使用系统原有的一些方法查询MongoDB感觉很费力,用起来也不爽,所以私下里就 ...
- 阿里云 访问控制RAM
https://help.aliyun.com/product/28625.html 为用户分配最小权限 别名主要用于 RAM 用户登录以及成功登录后的显示名. 强烈建议您给主账号绑定多因素认证. 设 ...
- Webkit二:RenderTree 创建
RenderObject 作为所有Render 树节点的基类,完全类似与DOM树中的Node基类,它是构成Render 树的基础,作用非比寻常,其中包含了构成Render 树所可能涉及到的一些基本属性 ...
- 九度OJ 1141:Financial Management (财务管理) (平均数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...
- kong
https://docs.konghq.com/install/centos/ wget https://bintray.com/kong/kong-community-edition-rpm/dow ...
- Android Button Maker(在线生成android shape xml文件的工具),真方便!
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/scry5566/article/details/25379275 直接上地址:http ...