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 ...
随机推荐
- 算法排序-lowB三人组
冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇
- 算法NB三人组
#快速排序-除了python自带的sort排序模块之外就这个最好用,只需会这个就行,其他的排序了解就好,能用冒泡,插入..的都可以用快排快速实现 import random from timewrap ...
- 关于input:-webkit-autofill样式问题
最近在整理项目的时候,遇到了一个chrome浏览器自动填充的样式问题, 用户名跟密码的input都设置为透明颜色,但是会变成黄色,打开chrome调试工具,发现有个input:-webkit-auto ...
- webpack打包问题
最近项目里需要替换一个logo,原先的logo打包后生成的静态文件里有对应的图片,替换了新的的图片打包后并没有生成相应的静态文件,两个图片都在同一个文件目录下,而且图片是直接引入并不会出现打包不到图片 ...
- Java解析Property文件
在Java项目中一些配置參数保存在Property文件里,这样能保证不改动原代码直接改动Property文件. PropertyParser.java package com.discover.par ...
- jquery live hover
$("table tr").live({ mouseenter: function() { //todo }, mouseleave: function() { //todo } ...
- Clustering of residential areas based on residential conditions
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3hpYW5neXVpYm0=/font/5a6L5L2T/fontsize/400/fill/I0 ...
- scrollify 全屏滚动插件
使用 1.引入文件 <script src="js/jquery.min.js"> </script> <script src="js/jq ...
- 关于indexOf的使用
今天项目中出现一个bug,在筛选数据的时候出现了冗余数据,查找发现在indexOf方法判断的时候找到了问题的所在. package demo; public class test { public s ...
- iOS信号量
引子: 在取本地联系人列表的时候看到同事用的这么一段代码: dispatch_semaphore_t sema = dispatch_semaphore_create(); ABAddressBook ...