4094: [Usaco2013 Dec]Optimal Milking

Description

Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 ... N,并排成一行。第i台挤奶机每天能够挤M(i)单位的牛奶 (1 < =M(i) <=100,000)。
由于机器间距离太近,使得两台相邻的机器不能在同一天使用。Farmer John可以自由选择不同的机器集合在不同的日子进行挤奶。
在D(1 < = D < = 50,000)天中,每天Farmer John对某一台挤奶机进行维护,改变该挤奶机的当天产量。
Farmer John希望设计一个挤奶方案,使得挤奶机能够在D天后获取最多的牛奶。

Input

第1行:两个整数N和D
第2..N+1行:每台挤奶机的M(i)
第N+2..N+D+1行:两个整数i和m,表示每天对机器i进行维护,机器i的产量为m。

Output

最大产量

Sample Input

5 3
1
2
3
4
5
5 2
2 7
1 10

Sample Output

32
【样例解释】
第1天,最优方案为2+4=6 ( 方案1+3+2一样)
第2天,最优方案为7+4=11
第3天,最优方案为10+3+2=15

Source

Gold

题解:

线段树。。

没什么好说的。。

发四种情况讨论

1.两边都选

2.左选右不选

3.左不选右选

4.都不选

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=40005;
#define p1 (p<<1)
#define p2 (p<<1|1)
int n,m,i,x,y,a[N],t[N<<2][4];
long long ans;
inline void read(int&a){
char c;
while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';
while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';
}
void pushup(int p)
{
t[p][0]=t[p][1]=t[p][2]=t[p][3]=0;
if(t[p1][0]!=-1&&t[p2][2]!=-1) t[p][0]=t[p1][0]+t[p2][2];
if(t[p1][1]!=-1&&max(t[p2][0],t[p2][2])!=-1) t[p][0]=max(t[p][0],t[p1][1]+max(t[p2][0],t[p2][2]));
if(t[p1][0]!=-1&&t[p2][3]!=-1) t[p][1]=t[p1][0]+t[p2][3];
if(t[p1][1]!=-1&&max(t[p2][1],t[p2][3])!=-1) t[p][1]=max(t[p][1],t[p1][1]+max(t[p2][1],t[p2][3]));
if(t[p1][2]!=-1&&t[p2][2]!=-1) t[p][2]=t[p1][2]+t[p2][2];
if(t[p1][3]!=-1&&max(t[p2][0],t[p2][2])!=-1) t[p][2]=max(t[p][2],t[p1][3]+max(t[p2][0],t[p2][2]));
if(t[p1][2]!=-1&&t[p2][3]!=-1) t[p][3]=t[p1][2]+t[p2][3];
if(t[p1][3]!=-1&&max(t[p2][1],t[p2][3])!=-1) t[p][3]=max(t[p][3],t[p1][3]+max(t[p2][1],t[p2][3]));
}
void build(int l,int r,int p)
{
if(l==r)
{
t[p][0]=a[l];
t[p][3]=0;t[p][1]=t[p][2]=-1;
return;
}
int mid=(l+r)>>1;
build(l,mid,p1);build(mid+1,r,p2);
pushup(p);
}
void update(int l,int r,int x,int y,int p)
{
if(l==r)
{
t[p][0]=y;
return;
}
int mid=(l+r)>>1;
if(x<=mid) update(l,mid,x,y,p1);else update(mid+1,r,x,y,p2);
pushup(p);
}
int main()
{
read(n),read(m);
for(i=1;i<=n;i++) read(a[i]);
build(1,n,1);
while(m--)
{
read(x),read(y);
update(1,n,x,y,1);
ans+=max(max(t[1][0],t[1][1]),max(t[1][2],t[1][3]));
}
cout<<ans;
return 0;
}

  

bzoj 4094: [Usaco2013 Dec]Optimal Milking的更多相关文章

  1. 【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树

    [BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...

  2. bzoj 4097: [Usaco2013 dec]Vacation Planning

    4097: [Usaco2013 dec]Vacation Planning Description Air Bovinia is planning to connect the N farms (1 ...

  3. BZOJ 4094 USACO 2013 Dec. Optimal Milking

    线段树 每个节点保存4个值,both表示左右端点都取,neither表示左右端点都不取,left表示只取左端点,right表示只取右端点. 维护的特殊姿势: $cur$的$both=max(ls.l+ ...

  4. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  5. POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  6. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. poj Optimal Milking

    Optimal Milking 题目: 有K个机器.C仅仅牛.要求求出最全部牛到各个产奶机的最短距离.给出一个C+K的矩阵,表示各种标号间的距离. 而每一个地方最多有M仅仅牛. 算法分析: 二分+最短 ...

  8. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  9. 题解 最优的挤奶方案(Optimal Milking)

    最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...

随机推荐

  1. 【转】CentOS7 yum方式配置LAMP环境

    采用Yum方式搭建: Apache+Mysql+PHP环境 原文地址: http://www.cnblogs.com/zutbaz/p/4420791.html 1.安装Apache yum inst ...

  2. H5小游戏——看你有多色

    使用了封装了canvas的create.js库来实现的. 最终效果: 工程: Rect.js /* * 方块类 */ function Rect(n,color,specialColor){ crea ...

  3. Sublime删除项目删不掉?

    最近用sublime进行项目的开发,感觉懵逼的像个小白菜~~ 今天遇到的问题可是一个超级白痴的问题,sublime的空白项目文件夹怎么都删不掉,我的傻逼操作是:选中文件--->Delete--- ...

  4. 小程序_请求封装network

    在utils目录下创建network.js文件封装请求 封装的network.js: //模块一,全局变量 let urlList = { host: 'http://47.106.25.53/', ...

  5. 服务器部署之nginx的配置

    nginx可作为Web和 反向代理 服务器,在高连接并发的情况下,Nginx是Apache服务器不错的替代品.下面记录一下自己对nginx的配置和使用. nginx的安装 环境:oracle-linu ...

  6. ubuntu之安装pycharm编辑器

    pycharm是Java写的,运行需要Java环境. 安装java jdk sudo add-apt-repository ppa:webupd8team/java sudo apt-get upda ...

  7. Linux-Load Average解析(转)

    load Average 1.1:什么是Load?什么是Load Average?   Load 就是对计算机干活多少的度量(WikiPedia:the system Load is a measur ...

  8. ssh连接不上排查方法总结

    //常见报错信息 # No route to host --> server端没有开机或是网络不通(这个原因很多,最简单的是网线没有插.还有就是可能会是网卡down了等) 如果是网卡down了i ...

  9. Tutorial 5: Relationships & Hyperlinked APIs

    转载自:http://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/ Tutorial 5: ...

  10. substring() slice() substr()的区别联系

    例如:var str='q1207526854' str.substring(form,to):从字符串里截取下标为form到下标为to的字符串(不包括to对应的字符)alert(str.substr ...