其他分享
首页 > 其他分享> > USACO Optimal Milking

USACO Optimal Milking

作者:互联网

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