UVaLive 3983 Robotruck (DP + 单调队列)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 + 单调队列)的主要内容,如果未能解决你的问题,请参考以下文章

LA3983 Robotruck

uva3983

D - Guess UVALive - 4255 拓扑排序

UVAlive4097 Yungom(思路)

UValive4195 Heroes of Money and Magic

UVALive 6530 Football (水