ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

USACO Optimal Milking

2020-10-22 21:02:20  阅读:361  来源: 互联网

标签:int mid USACO milk Farmer Milking Optimal John day


USACO Optimal Milking

洛谷传送门

JDOJ传送门

Description

Problem 2: Optimal Milking [Brian Dean, 2013]

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

Input

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

Output

* Line 1: The maximum total amount of milk FJ can produce over D days.

Sample Input

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

Sample Output

32


题解:

思路:单点修改+求最大点权独立集。

考虑用区间树维护。但是一时半会没明白要维护什么。(维护了个寂寞)

后来发现,但凡不是裸的线段树,都可以先考虑分类讨论+向上pushup区间合并。

我们完全可以维护区间端点有没有被选的情况,然后在pushup函数中分类转移即可。

C语言代码:

#include<stdio.h>
#define max(a,b) (a)>(b)?(a):(b)
#define lson pos<<1
#define rson pos<<1|1
#define int long long
int n,m,q,ans;
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57)
    {
        if(ch=='-')
            f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57)
        x=x*10+ch-48,ch=nc();
   	return x*f;
}
struct node
{
	int f[2][2],ans;
}tree[40005<<2];
void pushup(int pos)
{
	tree[pos].f[0][0]=max(tree[rson].f[0][1]+tree[lson].f[0][0],tree[rson].f[0][0]+tree[lson].f[1][0]);
	tree[pos].f[0][1]=max(tree[rson].f[0][1]+tree[lson].f[0][1],tree[rson].f[0][0]+tree[lson].f[1][1]);
	tree[pos].f[1][0]=max(tree[rson].f[1][0]+tree[lson].f[1][0],tree[rson].f[1][1]+tree[lson].f[0][0]);
	tree[pos].f[1][1]=max(tree[rson].f[1][1]+tree[lson].f[0][1],tree[rson].f[1][0]+tree[lson].f[1][1]);
	tree[pos].ans=max(tree[pos].f[0][0],tree[pos].f[1][1]);
	tree[pos].ans=max(tree[pos].ans,max(tree[pos].f[0][1],tree[pos].f[1][0]));
}
void build(int pos,int l,int r)
{
	if (l==r)
    {
		tree[pos].f[1][1]=tree[pos].ans=read();
		return;
	}
	int mid=(l+r)>>1;
	build(rson,l,mid); 
    build(lson,mid+1,r);
	pushup(pos);
}
void update(int pos,int l,int r,int x,int v)
{
	if (l==r)
    {
		tree[pos].f[1][1]=tree[pos].ans=v;
		return;
	}
	int mid=(l+r)>>1;
	if (mid>=x) 
        update(rson,l,mid,x,v);
	else 
        update(lson,mid+1,r,x,v);
	pushup(pos);	
}
signed main()
{
	n=read();q=read();
	build(1,1,n);
	for(int i=1;i<=q;i++)
    {
		int x=read(),y=read();
		update(1,1,n,x,y);
		ans+=tree[1].ans;
	}
	printf("%lld",ans);
	return 0;
}

标签:int,mid,USACO,milk,Farmer,Milking,Optimal,John,day
来源: https://www.cnblogs.com/fusiwei/p/13860948.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有