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 ...
随机推荐
- Linux高端内存
Linux高端内存是针对物理内存来说的,虚拟内存没有高端这个概念.Linux系统将虚拟内存分为两个部分,即用户地 址空间和内核地址空间,对于32位系统来说,虚拟地址空间为4GB,其中用户空间范围为0- ...
- angular 指令封装弹出框效果
就直接用bs的警告框啦~,Duang~ 功能 可以设置message和type,type就是bs内置的几种颜色 默认提示3秒框自动关闭,或者点击x号关闭 代码 模板 <div class=&qu ...
- 在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
- 【BZOJ4407】于神之怒加强版 莫比乌斯反演
[BZOJ4407]于神之怒加强版 Description 给下N,M,K.求 Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行, ...
- 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
[BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input ...
- linux SVN 安装配置
svn服务器有2种运行方式 1.独立服务器 (例如:svn://xxx.com/xxx):2.借助apache.(例如:http://svn.xxx.com/xxx):为了不依赖apache,选择第一 ...
- 【甘道夫】Eclipse+Maven搭建HBase开发环境及HBaseDAO代码演示样例
环境: Win764bit Eclipse Version: Kepler Service Release 1 java version "1.7.0_40" 第一步:Eclips ...
- dialog更新数据
将数据显示在最上面
- OC浅析一
Objective-C是一门简单的语言,95%是C.只是在语言层面上加了些关键字和语法.真正让Objective-C如此强大的是它的运行时.它很小但却很强大.它的核心是消息分发. 在Objective ...
- 模拟等待事件:db file sequential read
相关知识: db file sequential read 单块读 optimizer_index_cost_adj 这个初始化参数代表一个百分比,取值范围在1到10000之间.该参数表示索引扫描和 ...