题意:有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 + 单调队列)的更多相关文章

  1. UVALive 3983 Robotruck (单调队列,dp)

    如果状态定义为序号和重量的话,决策就是下一个垃圾捡或者不减,但是状态数太多了. 如果只定义序号作为状态的话,决策就变成从前面的某个j一直捡到i才送回垃圾. 这就变成了一个区间选最小值的问题,用单调队列 ...

  2. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

  3. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  4. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  5. 习题:烽火传递(DP+单调队列)

    烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...

  6. (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  7. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  8. bzoj2500: 幸福的道路(树形dp+单调队列)

    好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...

  9. URAL 1427. SMS(DP+单调队列)

    题目链接 我用的比较传统的办法...单调队列优化了一下,写的有点搓,不管怎样过了...两个单调队列,存两个东西,预处理一个标记数组存... #include <iostream> #inc ...

随机推荐

  1. Web性能测试工具:Siege安装&使用简介

    在Web性能测试工具中,siege是比较热门和常见的,它有安装简单,使用简单,测试报告详细的特点. 并且可以在文本中预定义一系列待测试url模拟,并可设定一定并发量下持续指定时间or测试进行测试. 比 ...

  2. 在JS中将JSON的字符串解析成JSON数据格式

    使用eval函数来解析 <script> var data="{root: [{name:'1',value:'0'},{name:'6101',value:'北京市'},{na ...

  3. SAM4E单片机之旅——2、LED闪烁之轮询定时器

    之前我们使用空循环,达到了延迟的目的,但是这样子的延迟比较不精确.现在就使用实时定时器(RTT)来进行更为精确的计时.RTT虽然不是特别通用,在某些单片机上可能没有,但它较为简单. RTT内部有一个计 ...

  4. Python: lambda, map, reduce, filter

    在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...

  5. java编程之JDBC

    JDBC的常用类和接口 1.       DriverManager类 管理数据库中的所有驱动程序,其所有的方法都是静态方法,调用时无需实例化,通过类名就可以直接调用. 2.       Connec ...

  6. NET 并发编程

    场景并发调用API 1.简单封装httpclient public class CommonHelper { private static readonly HttpClient _httpClien ...

  7. (非原)SQL注入专题--整理帖 && like 语句拼sql 如何防止注入攻击。

    原地址:blog.csdn.net/lvjin110/article/details/28697695 like 语句拼sql 如何防止注入攻击?http://bbs.csdn.net/topics/ ...

  8. ubuntu动态加载模块简单模板

    1:简单代码 #include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); ...

  9. Android 如何永久性开启adb 的root权限【转】

    本文转载自:https://www.2cto.com/kf/201702/593999.html adb 的root 权限是在system/core/adb/adb.c 中控制.主要根据ro.secu ...

  10. 使用eclipse的SVN连接码云

    码云配置: 码云的项目上,启用SVN访问 eclipse的配置,不配置这个会报错