bzoj 4094: [Usaco2013 Dec]Optimal Milking
4094: [Usaco2013 Dec]Optimal Milking
Description
Input
Output
Sample Input
1
2
3
4
5
5 2
2 7
1 10
Sample Output
【样例解释】
第1天,最优方案为2+4=6 ( 方案1+3+2一样)
第2天,最优方案为7+4=11
第3天,最优方案为10+3+2=15
Source
题解:
线段树。。
没什么好说的。。
发四种情况讨论
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的更多相关文章
- 【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树
[BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...
- bzoj 4097: [Usaco2013 dec]Vacation Planning
4097: [Usaco2013 dec]Vacation Planning Description Air Bovinia is planning to connect the N farms (1 ...
- BZOJ 4094 USACO 2013 Dec. Optimal Milking
线段树 每个节点保存4个值,both表示左右端点都取,neither表示左右端点都不取,left表示只取左端点,right表示只取右端点. 维护的特殊姿势: $cur$的$both=max(ls.l+ ...
- Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...
- POJ2112 Optimal Milking (网络流)(Dinic)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ 2112 Optimal Milking (二分+最短路径+网络流)
POJ 2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS Memory Limit: 30000K To ...
- poj Optimal Milking
Optimal Milking 题目: 有K个机器.C仅仅牛.要求求出最全部牛到各个产奶机的最短距离.给出一个C+K的矩阵,表示各种标号间的距离. 而每一个地方最多有M仅仅牛. 算法分析: 二分+最短 ...
- POJ 2112 Optimal Milking (二分 + floyd + 网络流)
POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...
- 题解 最优的挤奶方案(Optimal Milking)
最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec 内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...
随机推荐
- 在mac上安装ruby
1.先装RVM,一个多版本ruby环境的管理和切换工具 curl -sSL https://get.rvm.io | bash -s stable 会自动装上最新版.更新RVM版本:$ rvm get ...
- INIT_WORK
借助runtime pm,在需要使用模块时,增加引用计数(可调用pm_runtime_get),不需要使用时,减少引用计数(可调用pm_runtime_put). 1.INIT_WORK(struct ...
- windos8设置cpu数量和内存大小
转自:http://smilejay.com/2012/03/windows_cpu_memory_setting/ Windows 8(测试版)在作为Xen Guest中的benchmark测试.我 ...
- 135.Candy---贪心
题目链接 题目大意:分糖果,每个小朋友都有一个ratings值,且每个小朋友至少都要有一个糖果,而且每个小朋友的ratings值如果比左右邻舍的小朋友的ratings值高,则其糖果数量也比邻舍的小朋友 ...
- Transport failed: java.io.EOFException
ActiveMQ服务端有时会报Transport failed: java.io.EOFException这样的错误,这是由客户端丢失连接并反复尝试连接导致的,不是什么大问题,有时网络波动就会产生这种 ...
- PSQueue队列操作
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...
- 查看压缩包内容tar -tf
linux 压缩文件内容查看 分类:Linux | 标签: linux 压缩文件内容查看 2012-03-14 22:01阅读(1243)评论(0) 1. zipinfo 执行zipinfo ...
- 看懂 MySQL 慢查询日志
MySQL中的日志包括: 错误日志.二进制日志.通用查询日志.慢查询日志等等. 这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1)通用查询日志:记录建立的客户端连接和执行的语句. 2) ...
- sicily 1154. Easy sort (tree sort& merge sort)
Description You know sorting is very important. And this easy problem is: Given you an array with N ...
- [实战]MVC5+EF6+MySql企业网盘实战(19)——BJUI和ztree
写在前面 上周在博客园看到一篇通用权限系统的文章,看到他那个UI不错,这里就研究了一下,将网盘的UI修改为他的那个,感兴趣的可以参考:http://b-jui.com/ 系列文章 [EF]vs15+e ...