[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.

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

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

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

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

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

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

define:lc=(u<<1),rc=(u<<1∣1)define:lc=(u<<1),rc=(u<<1|1)define:lc=(u<<1),rc=(u<<11)
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][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][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]=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][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][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]=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][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][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]=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][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][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]=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∈{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\})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{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;
}
### Feeding the Cows B 问题解析 在《USACO 2022年12月比赛》的 Silver 组第二题中,题目要求解决一个关于奶牛喂养的问题。具体描述如下: 输入包括一个长度为 $ n $ 的字符串,表示一排奶牛,其中每个字符为 'C' 或 'W',分别表示该位置有一头奶牛或是一片草地。目标是通过最少的操作次数,使得每头奶牛('C')都能在其左侧或右侧至少有一个相邻的草地('W'),以便能够被喂养。每次操作可以将一个 'C' 变成 'W' 或者将一个 'W' 变成 'C'。 输出为最小的操作次数,若无法满足条件则输出 -1。 #### 问题分析 1. **问题条件**: - 每个奶牛必须在其左右至少有一个相邻的草地。 - 每次操作可以修改一个字符('C' <-> 'W')。 - 需要找出最小操作次数。 2. **贪心策略**: - 从左到右遍历字符串,当遇到一个奶牛('C')时,检查其右侧是否有一个草地('W'),如果存在,将该草地变为奶牛的“喂养点”。 - 如果右侧没有草地,则需要修改当前奶牛或其右侧的某个字符以满足条件。 3. **实现逻辑**: - 遍历字符串,维护一个指针,标记当前可以使用的草地位置。 - 如果当前字符是 'C',且其右侧没有可用草地,则需要修改一个字符。 - 记录每次操作,并确保最终所有奶牛都能被喂养。 #### 示例代码实现 ```cpp #include <iostream> #include <string> using namespace std; int main() { int n; string s; cin >> n >> s; int operations = 0; int lastGrass = -1; for (int i = 0; i < n; ++i) { if (s[i] == 'C') { if (lastGrass == -1 || lastGrass < i - 1) { // Check if there is a grass to the right bool found = false; for (int j = i + 1; j < n; ++j) { if (s[j] == 'W') { lastGrass = j; found = true; break; } } if (!found) { cout << -1 << endl; return 0; } } lastGrass = i + 1; // Mark the next available grass position continue; } else if (s[i] == 'W') { lastGrass = i; } } cout << operations << endl; return 0; } ``` #### 时间复杂度 - 该算法的时间复杂度为 $ O(n) $,因为每个字符最多被访问两次(一次遍历,一次查找草地)。 #### 空间复杂度 - 空间复杂度为 $ O(1) $,仅使用了常量级的额外空间。 #### 注意事项 - 需要处理边界情况,例如字符串末尾没有草地。 - 如果无法满足条件(即存在无法喂养的奶牛),应输出 -1。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值