[P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub
题目描述
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.
输入输出样例
5 3
1
2
3
4
5
5 2
2 7
1 10
32
说明
There are 5 machines, with initial milk outputs 1,2,3,4,5. On day 1, machine 5 is updated to output 2 unit of milk, and so on.
On day one, the optimal amount of milk is 2+4 = 6 (also achievable as 1+3+2). On day two, the optimal amount is 7+4 = 11. On day three, the optimal amount is 10+3+2=15.
题意简述:给定n个点排成一排,每个点有一个点权,多次改变某个点的点权并将最大点独立集计入答案,输出最终的答案
每次修改就是线段树单点修改操作,只需要对每个节点维护f数组就好了
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std; const int maxn=4e4+;
int n,d;
ll ans;
int a[maxn];
struct Tree
{
int l,r;
ll f[][];
}t[maxn<<];
inline int read()
{
char ch=getchar();
int s=,f=;
while (!(ch>=''&&ch<='')){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
void update(int rt)
{
for (int i=;i<=;i++)
for (int j=;j<=;j++)
{
ll p=max(t[rt<<].f[i][]+t[rt<<|].f[][j],t[rt<<].f[i][]+t[rt<<|].f[][j]);
p=max(p,t[rt<<].f[i][]+t[rt<<|].f[][j]);
t[rt].f[i][j]=p;
}
}
void build(int rt,int l,int r)
{
t[rt].l=l;t[rt].r=r;
if (l==r)
{
t[rt].f[][]=1ll*a[l];
return;
}
int mid=l+r>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
update(rt);
}
void change(int rt,int x,int v)
{
if (t[rt].l==t[rt].r)
{
t[rt].f[][]=1ll*v;
return;
}
int mid=t[rt].l+t[rt].r>>;
if (x<=mid) change(rt<<,x,v);
else change(rt<<|,x,v);
update(rt);
}
int main()
{
n=read();d=read();
for (int i=;i<=n;i++) a[i]=read();
build(,,n);
for (int i=;i<=d;i++)
{
int x=read(),v=read();
change(,x,v);
ll p=;
for (int j=;j<=;j++)
for (int k=;k<=;k++)
p=max(p,t[].f[j][k]);
ans+=1ll*p;
}
printf("%lld\n",ans);
return ;
}
[P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)的更多相关文章
- P3097 [USACO13DEC]最优挤奶Optimal Milking
P3097 [USACO13DEC]最优挤奶Optimal Milking 题意简述:给定n个点排成一排,每个点有一个点权,多次改变某个点的点权并将最大点独立集计入答案,输出最终的答案 感谢@zht4 ...
- 洛谷P3097 - [USACO13DEC]最优挤奶Optimal Milking
Portal Description 给出一个\(n(n\leq4\times10^4)\)个数的数列\(\{a_n\}(a_i\geq1)\).一个数列的最大贡献定义为其中若干个不相邻的数的和的最大 ...
- 【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树
[BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...
- 【USACO13DEC】 最优挤奶 - 线段树
题目描述 FJ最近买了1个新仓库, 内含N 个挤奶机,1 到N 编号并排成一行. 挤奶机i 每天能产出M(i) 单位的奶.不幸的是, 机器装得太近以至于如果一台机器i 在某天被使用, 那与它相邻的两台 ...
- 【LeetCode】553. Optimal Division 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 题解 最优的挤奶方案(Optimal Milking)
最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec 内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...
- P3097 [USACO13DEC]最优挤奶(线段树优化dp)
盲猜dp系列... 题意:给定序列,选了i就不能选与i相邻的两个,求最大值,带修改 蒟蒻在考场上10min打完以为只有两种情况的错解...居然能骗一点分... 先讲下当时的思路吧. f[i][0/1] ...
- Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 19347 Accepted: 690 ...
- bzoj 4094: [Usaco2013 Dec]Optimal Milking
4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...
随机推荐
- hdu5351
题目名称:MZL's Border 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5351 题意:给出fib 1 = b,fib2 = a ; fib ...
- 零基础学HTML 5实战开发(第一季)
開始学习html5了.趋势不得不学习啊,之前老毛说过落后就要挨打,如今是不学习就要被市场淘汰,被社会淘汰.喜欢挑战,喜欢冒险.来吧.csdn给我们提供了那么好的平台.用起来..零基础学HTML 5的实 ...
- wikioi 1306 机智Trie树
题目描写叙述 Description 看广播操无聊得非常~你有认为吗?在看广播操一波又一波的人潮涌过再退去.认为非常没意思--于是,偶们的大神犇JHT发明了一个及其好玩的游戏~ 把每一班级的队形看成一 ...
- codeblocks的c程序目录结构与执行过程
执行过程 编译 形成 .o .obj 连接 形成.exe文件 执行 目录结构 主程序main.c #include <stdio.h> #include <stdlib.h> ...
- ARIMA模型实例讲解——网络流量预测可以使用啊
ARIMA模型实例讲解:时间序列预测需要多少历史数据? from:https://www.leiphone.com/news/201704/6zgOPEjmlvMpfvaB.html 雷锋网按:本 ...
- Pandas与Matplotlib
Pandas与Matplotlib基础 pandas是Python中开源的,高性能的用于数据分析的库.其中包含了很多可用的数据结构及功能,各种结构支持相互转换,并且支持读取.保存数据.结合matplo ...
- VMware虚拟机的CentOS7安装Nginx后本机用CentOS的IP地址无法访问
因为CentOS7的默认防火墙改成了Firewall,不再使用iptables为默认防火墙了 所以需要使用以下命令添加80端口 firewall-cmd --zone=public --add-por ...
- DBMS_XPLAN详细说明
执行计划的组成部分 正确的看执行计划 DBMS_XPLAN 这个包是一个很好查看执行计划,显示很多格式,来分析执行计划中存在的问题 format:控制详细执行计划输出的格式,包含以下内容: BASIC ...
- Java基础——protected访问修饰符探讨
Java基础——protected访问修饰符探讨 根据官方说法:(如图) protected修饰符是可以修饰其他包中的子孙类的,但是我做了个实验,结果发现了一个有趣的现象! 具体请往下看: packa ...
- hadoop 编译自己的jar包并运行
我修从网上找了份java代码 我为了让它在hadoop下跑起来居然花了两个多小时... 首先最好不要在java代码中设置package...使用default package即可... 然后在java ...