bzoj4094 [Usaco2013 Dec]Optimal Milking 线段树

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/80695716

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天后获取最多的牛奶。

Solution


我非常的菜因此没有秒掉这道题,一开始居然忽略数据范围脑补网络流的搞法

开一棵线段树,每个节点记录这段区间分别只要左端点、只要右端点、两边都要、两边都不要的最大值,然后分类上传即可

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const int N=40005;

LL s1[N<<2],s2[N<<2],s3[N<<2],s4[N<<2];

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void update(LL &x,LL y) {
    x=std:: max(x,y);
}

void modify(int now,int tl,int tr,int x,int v) {
    if (tl==tr) {
        s3[now]=v;
        return ;
    }
    int mid=(tl+tr)>>1;
    if (x<=mid) modify(now<<1,tl,mid,x,v);
    else modify(now<<1|1,mid+1,tr,x,v);
    s1[now]=0;
    update(s1[now],s1[now<<1]+s1[now<<1|1]);
    update(s1[now],s1[now<<1]+s4[now<<1|1]);
    update(s1[now],s3[now<<1]+s4[now<<1|1]);

    s2[now]=0;
    update(s2[now],s2[now<<1]+s2[now<<1|1]);
    update(s2[now],s4[now<<1]+s2[now<<1|1]);
    update(s2[now],s4[now<<1]+s3[now<<1|1]);

    s3[now]=0;
    update(s3[now],s1[now<<1]+s2[now<<1|1]);
    update(s3[now],s1[now<<1]+s3[now<<1|1]);
    update(s3[now],s3[now<<1]+s2[now<<1|1]);

    s4[now]=0;
    update(s4[now],s2[now<<1]+s4[now<<1|1]);
    update(s4[now],s4[now<<1]+s1[now<<1|1]);
    update(s4[now],s4[now<<1]+s4[now<<1|1]);
}

int main(void) {
    int n=read(),T=read(); LL ans=0;
    rep(i,1,n) {
        int x=read();
        modify(1,1,n,i,x);
    }
    for (;T--;) {
        int x=read(),v=read();
        modify(1,1,n,x,v);
        ans+=std:: max(s1[1],std:: max(s2[1],std:: max(s3[1],s4[1])));
    }
    printf("%lld\n", ans);
    return 0;
}

Optimal Milking

12-01

DescriptionnFJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C. nnEach milking point can "process" at most M (1 <= M <= 15) cows each day. nnWrite a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine. nInputn* Line 1: A single line with three space-separated integers: K, C, and M. nn* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line. nOutputnA single line with a single integer that is the minimum possible total distance for the furthest walking cow. nSample Inputn2 3 2n0 3 2 1 1n3 0 3 2 0n2 3 0 1 0n1 2 1 0 2n1 0 0 2 0nSample Outputn2

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭