当前位置: 首页 > news >正文

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

输入输出样例

输入样例#1: 
5 3 
1 
2 
3 
4 
5 
5 2 
2 7 
1 10 
输出样例#1: 
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+15;
int n,d;
ll ans;
int a[maxn];
struct Tree
{
    int l,r;
    ll f[2][2];
}t[maxn<<2];
inline int read()
{
    char ch=getchar();
    int s=0,f=1;
    while (!(ch>='0'&&ch<='9')){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
    return s*f;
}
void update(int rt)
{
    for (int i=0;i<=1;i++)
        for (int j=0;j<=1;j++)
        {
            ll p=max(t[rt<<1].f[i][0]+t[rt<<1|1].f[0][j],t[rt<<1].f[i][1]+t[rt<<1|1].f[0][j]);
            p=max(p,t[rt<<1].f[i][0]+t[rt<<1|1].f[1][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[1][1]=1ll*a[l];
        return;
    }
    int mid=l+r>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    update(rt);
}
void change(int rt,int x,int v)
{
    if (t[rt].l==t[rt].r)
    {
        t[rt].f[1][1]=1ll*v;
        return;
    }
    int mid=t[rt].l+t[rt].r>>1;
    if (x<=mid) change(rt<<1,x,v);
    else change(rt<<1|1,x,v);
    update(rt);
}
int main()
{
    n=read();d=read();
    for (int i=1;i<=n;i++) a[i]=read();
    build(1,1,n);
    for (int i=1;i<=d;i++)
    {
        int x=read(),v=read();
        change(1,x,v);
        ll p=0;
        for (int j=0;j<=1;j++)
            for (int k=0;k<=1;k++)
                p=max(p,t[1].f[j][k]);
        ans+=1ll*p;
    }
    printf("%lld\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/xxzh/p/9351305.html

相关文章:

  • build custom centos7
  • ES系列二、CentOS7安装ES head6.3.1
  • 对整型、浮点型、字符串类型的认识
  • DNS域名解析中A、AAAA、CNAME、MX、NS、TXT、SRV、SOA、PTR各项记录的作用
  • 交互式数据处理
  • 在项目中直接执行里面的文件
  • Redis监听回调
  • P1064 金明的预算方案
  • windows C:\documents and settings拒绝访问
  • 枚举+最短路 poj1062
  • Python——requests模块
  • GitLab领取任务+建立分支+本地修改+上传分支+合并分支详细步骤
  • Win10应用商店缓存信息多如何去清理?
  • [数位DP][CQOI2016]手机号码(附数位DP模板)
  • SpringBoot | 第十一章:Redis的集成和简单使用
  • AWS实战 - 利用IAM对S3做访问控制
  • canvas绘制圆角头像
  • FastReport在线报表设计器工作原理
  • iOS 系统授权开发
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • MySQL几个简单SQL的优化
  • overflow: hidden IE7无效
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • vue中实现单选
  • 对象管理器(defineProperty)学习笔记
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 悄悄地说一个bug
  • 手写一个CommonJS打包工具(一)
  • 硬币翻转问题,区间操作
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • # 数据结构
  • #### go map 底层结构 ####
  • #include到底该写在哪
  • #预处理和函数的对比以及条件编译
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (搬运以学习)flask 上下文的实现
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (三) diretfbrc详解
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十)T检验-第一部分
  • (四) 虚拟摄像头vivi体验
  • *** 2003
  • ***监测系统的构建(chkrootkit )
  • .gitignore文件—git忽略文件
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net和php怎么连接,php和apache之间如何连接
  • .NET开发人员必知的八个网站
  • .NET项目中存在多个web.config文件时的加载顺序
  • .sh 的运行
  • .sys文件乱码_python vscode输出乱码
  • /bin/rm: 参数列表过长"的解决办法
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @vue/cli脚手架