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. NYOJ 221 Tree (二叉树)

    题目链接 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructi ...

  2. NYOJ 136 等式 (哈希)

    题目链接 描述 有以下等式:a1x13+a2x23+a3x33+a4x43+a5*x53=0 x1,x2,x3,x4,x5都就在区间[-50,50]之间的整数,且x1,x2,x3,x4,x5都不等于0 ...

  3. KEA128单片机启动代码分析

    ;/*****************************************************************************; * @file: startup_SK ...

  4. 贪心算法_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...

  5. linux 下 genymotion 模拟器不能安装app

    提示: "应用未安装" 解决方法: 下载: Genymotion-ARM-Translation_v1.1.zip 进入genymotion 的tools用adb传进去: ./ad ...

  6. 【Python学习】csv库

    csv(Comma-Separated Values, 逗号分割值)是存储表格数据的常用文件格式. 它每一行都用一个换行符分隔,列与列之间用逗号分隔. 本地文件 Python的csv库可以非常简单地修 ...

  7. 64_n2

    nodejs-from-0.1.3-4.fc26.noarch.rpm 11-Feb-2017 15:01 9982 nodejs-from2-2.1.0-6.fc26.noarch.rpm 11-F ...

  8. java处理金证中登查询图片二进制流问题

    package com.szkingdom.kess.model; import java.io.File; import java.io.FileOutputStream; import java. ...

  9. c++各种排序的简单实现

    /* 直插排序 */ void InsertSort(vector<int> &arr){ for(int i = 1;i < arr.size();++i){ for(in ...

  10. springboot + swagger2 生成api文档

    直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...