[USACO13DEC] 最优挤奶Optimal Milking

题目描述

Farmer John has recently purchased a new barn containing N milking machines (1 <= N <= 40,000), conveniently numbered 1…N and arranged in a row.

Milking machine i is capable of extracting M(i) units of milk per day (1 <= M(i) <= 100,000). Unfortunately, the machines were installed so close together that if a machine i is in use on a particular day, its two neighboring machines cannot be used that day (endpoint machines have only one neighbor, of course). Farmer John is free to select different subsets of machines to operate on different days.

Farmer John is interested in computing the maximum amount of milk he can extract over a series of D days (1 <= D <= 50,000). At the beginning of each day, he has enough time to perform maintenance on one selected milking machine i, thereby changing its daily milk output M(i) from that day forward. Given a list of these daily modifications, please tell Farmer John how much milk he can produce over D days (note that this number might not fit into a 32-bit integer).

FJ最近买了1个新仓库, 内含N 个挤奶机,1 到N 编号并排成一行。

挤奶机i 每天能产出M(i) 单位的奶。不幸的是, 机器装得太近以至于如果一台机器i 在某天被使用, 那与它相邻的两台机器那一天不能被使用

(当然, 两端点处的机器分别只有一个与之相邻的机器)。

FJ 可自由选择不同的机器在不同的日子工作。

FJ感兴趣于计算在D 天内他能产出奶的最大值。在每天开始时, 他有足够的时间维护一个选中的挤奶机i, 从而改变它从那天起的每日产奶量M(i)。

给出这些每日的修改,请告诉FJ他D 天中能产多少奶。

输入格式

  • Line 1: The values of N and D.

  • Lines 2…1+N: Line i+1 contains the initial value of M(i).

  • Lines 2+N…1+N+D: Line 1+N+d contains two integers i and m,

indicating that Farmer John updates the value of M(i) to m at the beginning of day d.

输出格式

  • Line 1: The maximum total amount of milk FJ can produce over D days.

这道题很明显能够看出来是一个 d p dp ,由于有修改,所以这道题是一个线段树,所以这道题是一个线段树上的 d p dp

我们对于线段树的每一个节点,都开一个 d p dp 数组

对于 u u 节点, f [ u ] [ i ] [ j ] , i , j { 0 , 1 } f[u][i][j],i,j\in\{ 0,1\} 表示 u u 节点,左节点选(不选),右节点选(不选)时的最大值

初值就是对于每个叶子结点, f [ u ] [ 1 ] [ 1 ] = a [ l ] f[u][1][1]=a[l] 就可以了

但是因为相邻的两个不能同时选,所以我们在向上 p u s h u p pushup 的时候,不能让左子树的右端点和右子树的左端点同时被选上

那么我们可以得到一个非常非常长的转移方程

d e f i n e : l c = ( u < < 1 ) , r c = ( u < < 1 1 ) define:lc=(u<<1),rc=(u<<1|1)
f [ u ] [ 0 ] [ 0 ] = m a x { f [ l c ] [ 0 ] [ 0 ] + f [ r c ] [ 0 ] [ 0 ] , f [ l c ] [ 0 ] [ 0 ] + f [ r c ] [ 1 ] [ 0 ] , f [ l c ] [ 0 ] [ 1 ] + f [ r c ] [ 0 ] [ 0 ] } f[u][0][0]=max\{ f[lc][0][0]+f[rc][0][0],f[lc][0][0]+f[rc][1][0],f[lc][0][1]+f[rc][0][0]\}
f [ u ] [ 0 ] [ 1 ] = m a x { f [ l c ] [ 0 ] [ 0 ] + f [ r c ] [ 0 ] [ 1 ] , f [ l c ] [ 0 ] [ 1 ] + f [ r c ] [ 0 ] [ 1 ] , f [ l c ] [ 0 ] [ 0 ] + f [ r c ] [ 1 ] [ 1 ] } f[u][0][1]=max\{f[lc][0][0]+f[rc][0][1],f[lc][0][1]+f[rc][0][1],f[lc][0][0]+f[rc][1][1]\}
f [ u ] [ 1 ] [ 0 ] = m a x { f [ l c ] [ 1 ] [ 0 ] + f [ r c ] [ 0 ] [ 0 ] , f [ l c ] [ 1 ] [ 0 ] + f [ r c ] [ 1 ] [ 0 ] , f [ l c ] [ 1 ] [ 1 ] + f [ r c ] [ 0 ] [ 0 ] } f[u][1][0]=max\{f[lc][1][0]+f[rc][0][0],f[lc][1][0]+f[rc][1][0],f[lc][1][1]+f[rc][0][0]\}
f [ u ] [ 1 ] [ 1 ] = m a x { f [ l c ] [ 1 ] [ 0 ] + f [ r c ] [ 0 ] [ 1 ] , f [ l c ] [ 1 ] [ 1 ] + f [ r c ] [ 0 ] [ 1 ] , f [ l c ] [ 1 ] [ 0 ] + f [ r c ] [ 1 ] [ 1 ] } f[u][1][1]=max\{f[lc][1][0]+f[rc][0][1],f[lc][1][1]+f[rc][0][1],f[lc][1][0]+f[rc][1][1]\}

其实可以整合一下(写完之后才发现)

f [ u ] [ i ] [ j ] = m a x { f [ l c ] [ i ] [ 0 ] + f [ r c ] [ 0 ] [ j ] , f [ l c ] [ i ] [ 0 ] + f [ r c ] [ 1 ] [ j ] , f [ l c ] [ i ] [ 1 ] + f [ r c ] [ 0 ] [ k ] } ( i , j { 0 , 1 } ) f[u][i][j]=max\{f[lc][i][0]+f[rc][0][j],f[lc][i][0]+f[rc][1][j],f[lc][i][1]+f[rc][0][k]\}(i,j\in\{0,1\})

# include <cstdio>
# include <algorithm>
# include <cstring>
# include <cmath>
# include <climits>
# include <iostream>
# include <string>
# include <queue>
# include <stack>
# include <vector>
# include <set>
# include <map>
# include <cstdlib>
# include <ctime>
using namespace std;

# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)

typedef long long ll;
const int N=1e5+5;
const int inf=0x7fffffff;
const double eps=1e-7;
template <typename T> void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
	x*=f;
}

# define int long long

int n,d,ans;
int a[N];

struct segment_tree{
	int l,r;
	int f[2][2];	
}seg[N<<2];

# define lc (u<<1)
# define rc (u<<1|1)

void pushup(int u){
	seg[u].f[0][0]=max(seg[lc].f[0][0]+seg[rc].f[0][0],max(seg[lc].f[0][0]+seg[rc].f[1][0],seg[lc].f[0][1]+seg[rc].f[0][0]));
	seg[u].f[1][0]=max(seg[lc].f[1][0]+seg[rc].f[0][0],max(seg[lc].f[1][0]+seg[rc].f[1][0],seg[lc].f[1][1]+seg[rc].f[0][0]));
	seg[u].f[0][1]=max(seg[lc].f[0][0]+seg[rc].f[0][1],max(seg[lc].f[0][0]+seg[rc].f[1][1],seg[lc].f[0][1]+seg[rc].f[0][1]));
	seg[u].f[1][1]=max(seg[lc].f[1][0]+seg[rc].f[1][1],max(seg[lc].f[1][1]+seg[rc].f[0][1],seg[lc].f[1][0]+seg[rc].f[1][1]));	
}

void build(int u,int l,int r){
	seg[u].l=l,seg[u].r=r;
	if(l==r){seg[u].f[1][1]=a[l];return;}
	int mid=l+r>>1;
	build(lc,l,mid);
	build(rc,mid+1,r);
	pushup(u);	
}

void update(int u,int x,int k){
	if(seg[u].l==seg[u].r){seg[u].f[1][1]=k;return;}
	int mid=seg[u].l+seg[u].r>>1;
	if(x<=mid)update(lc,x,k);
	else update(rc,x,k);
	pushup(u);	
}

signed main()
{
	read(n),read(d);
	Rep(i,1,n)read(a[i]);
	build(1,1,n);
	Rep(i,1,d){
		int x,y;
		read(x),read(y);
		update(1,x,y);
		ans+=max(max(seg[1].f[0][0],seg[1].f[1][1]),max(seg[1].f[0][1],seg[1].f[1][0]));
	}
	printf("%lld\n",ans);
	return 0;
}
发布了45 篇原创文章 · 获赞 52 · 访问量 9655

猜你喜欢

转载自blog.csdn.net/devout_/article/details/104107144
今日推荐