[NC1102B]货物分组

题目

传送门 to nowcoder

题目描述
V e n n \tt{Venn} Venn 要对货物打包。

每个货物有一定的重量,她可以用若干个箱子来装下所有的货物,但是每个箱子中物品重量总和不能超过 W W W

V e n n \tt{Venn} Venn 有一个独特的习惯,在装货过程中,某一个箱子里货物的编号必须是一个连续的区间,并且必需依次使用箱子按照物品的编号顺序装入,具体来讲编号为 1 1 1 的箱子一定包含 1 1 1 号物品,编号最大的箱子一定包含 n n n 号物品。

对于第 x x x 个箱子,如果里面装的货物总重量为 w w w ,那么费用为 x w xw xw

另外对于每一个箱子,在通过门卫时还会被收税,税款是箱子中重量最大的货物的重量减去重量最小的货物的重量。

她想知道,把所有货物打包并运过门卫所需要的最小费用为多少。注:收取的税款也算作费用。

输入格式
第一行两个整数 n , W n,W n,W ,分别表示货物个数以及每个箱子最大重量。

接下来一行 n n n 个正整数 a 1 , a 2 , a 3 , … , a n a_1,a_2,a_3,\dots,a_n a1,a2,a3,,an ,其中 a x a_x ax 表示编号为 x x x 的货物的重量。

输出格式
一行一个正整数表示最小费用。

数据范围与约定
对于 10 % 10\% 10% 的数据, n ≤ 10 n \leq 10 n10
对于 30 % 30\% 30% 的数据, n ≤ 500 n\leq 500 n500
对于 60 % 60\% 60% 的数据, n ≤ 5000 n\leq 5000 n5000
对于 100 % 100\% 100% 的数据, n ≤ 1 0 5 , 1 ≤ a i ≤ W n\leq 10^5 , 1\leq a_i \leq W n105,1aiW

思路

肯定动态规划啊!

暴力法

f ( x , y ) f(x,y) f(x,y) 表示前 x x x 个货物已经打包,用了 y y y 个箱子。

费用提前计算

这样只需要 f ( x ) f(x) f(x) 表示前 x x x 个货物已经被打包。

f ( x ) = min ⁡ y f ( y ) + c o s t ( y + 1 , x ) + ∑ k = x n a k f(x)=\min_y f(y)+{\tt cost}(y+1,x)+\sum_{k=x}^{n}a_k f(x)=yminf(y)+cost(y+1,x)+k=xnak

单调栈 × \times ×线段树

虽然 f ( y ) f(y) f(y) 递增、 c o s t ( y + 1 , x ) {\tt cost}(y\text{+}1,x) cost(y+1,x) 递减,没法用单调队列,但是可以用 线段树 硬求最小值!

考虑 g ( y ) = f ( y ) + c o s t ( y + 1 , x ) g(y)=f(y)+{\tt cost}(y\text{+}1,x) g(y)=f(y)+cost(y+1,x) 。因为 x x x 是变值,故 g ( y ) g(y) g(y) 随着 x x x 的变化而变化。

此时可以写成

f ( x ) = min ⁡ y g ( y ) + ∑ k = x n a k f(x)=\min_{y}g(y)+\sum_{k=x}^{n}a_k f(x)=yming(y)+k=xnak

如果用线段树维护 g g g ,就可以 O ( log ⁡ n ) \mathcal O(\log n) O(logn) 查询最小值转移了。

现在考虑 g g g 中的变值: c o s t ( y + 1 , x ) {\tt cost}(y\text{+}1,x) cost(y+1,x) 。这个值的变大,一定是一个区间变大相同的值。

因为 max ⁡ \max max(或 min ⁡ \min min)可以用 单调栈 维护——两个单调栈之间的元素的 max ⁡ \max max 值( min ⁡ \min min 值)都是一样的。

这样一来,区间内 max ⁡ \max max min ⁡ \min min)的变大(变小)是相同的,就可以使用区间修改。

题外话:这玩意儿很像珂朵莉哎!

由于是单调栈,总时间是 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn) 。加上动态规划的复杂度,总复杂度 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn)

代码

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
inline int readint(){
	int a; scanf("%d",&a); return a;
}

const int MaxN = 100000;
const long long infty = (1ll<<62)-1;

class SegmentTree{
	struct Node{
		int l, r;
		long long least, lazy;
		Node(){ least = infty; lazy = 0; }
	}node[MaxN<<2|3];
	void pushUp(int pos){
		node[pos].least = min(node[pos<<1].least,node[pos<<1|1].least);
	}
	void buildTree(int l,int r,int pos){
		node[pos].l = l, node[pos].r = r;
		if(l != r){
			buildTree(l,(l+r)>>1,pos<<1);
			buildTree((l+r)/2+1,r,pos<<1|1);
			pushUp(pos);
		}
	}
	void addNode(int pos,int addv){
		node[pos].lazy += addv;
		node[pos].least += addv;
	}
	void pushDown(int pos){
		addNode(pos<<1,node[pos].lazy);
		addNode(pos<<1|1,node[pos].lazy);
		node[pos].lazy = 0;
	}
public:
	void init(int n){
		buildTree(0,n,1);
	}
	void addRange(int l,int r,int addv,int pos=1){
		if(l <= node[pos].l and node[pos].r <= r)
			return addNode(pos,addv);
		pushDown(pos); int mid = (node[pos].l+node[pos].r)>>1;
		if(l <= mid) addRange(l,r,addv,pos<<1);
		if(r > mid) addRange(l,r,addv,pos<<1|1);
		pushUp(pos);
	}
	void addPoint(int id,int addv){
		return addRange(id,id,addv,1);
	}
	void modifyPoint(int id,long long v,int pos=1){
		if(node[pos].l == node[pos].r){
			node[pos].least = v;
			node[pos].lazy = 0;
			return ;
		}
		pushDown(pos); int mid = (node[pos].l+node[pos].r)>>1;
		if(id <= mid) modifyPoint(id,v,pos<<1);
		else modifyPoint(id,v,pos<<1|1);
		pushUp(pos);
	}
	long long queryRange(int l,int r,int pos=1){
		if(l <= node[pos].l and node[pos].r <= r)
			return node[pos].least;
		pushDown(pos); int mid = (node[pos].l+node[pos].r)>>1;
		if(r <= mid) return queryRange(l,r,pos<<1);
		if(l > mid) return queryRange(l,r,pos<<1|1);
		return min(queryRange(l,r,pos<<1),queryRange(l,r,pos<<1|1));
	}
};

int n, w, a[MaxN+2];
long long s[MaxN+2];

void init(){
	n = readint(), w = readint();
	for(int i=1; i<=n; ++i){
		a[i] = readint();
		s[i] = a[i]+s[i-1];
	}
}

long long dp[MaxN+2];
vector<int> maxV, minV;
SegmentTree ppl;
void changeMaxV(int id){
	int last, zxy;
	while(not maxV.empty() and a[maxV.back()] < a[id]){
		last = maxV.back(),	maxV.pop_back();
		zxy = (maxV.empty()?0:maxV.back());
		ppl.addRange(zxy,last-1,a[id]-a[last]);
	}
	maxV.push_back(id);
}
void changeMinV(int id){
	int last, zxy;
	while(not minV.empty() and a[minV.back()] > a[id]){
		last = minV.back(), minV.pop_back();
		zxy = (minV.empty()?0:minV.back());
		ppl.addRange(zxy,last-1,a[last]-a[id]);
	}
	minV.push_back(id);
}
void solve(){
	ppl.init(n);
	int leftBound = 0;
	long long sigma = s[n]; // 后缀和 
	for(int i=1; i<=n; ++i){
		ppl.modifyPoint(i-1,dp[i-1]);
		changeMaxV(i), changeMinV(i);
		ppl.addRange(0,i-1,a[i]);
		sigma -= a[i];
		while(s[i]-s[leftBound] > w)
			++ leftBound;
		long long splashing = ppl.queryRange(leftBound,i-1);
		dp[i] = splashing+sigma;
	}
	printf("%lld\n",dp[n]);
}

int main(){
	init(); solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值