题目传送门

据说\(NOIp\)前发题解可以\(\mathfrak{RP}\)++


因为要尽可能满足更多奶牛,所以按照这种区间贪心题的套路,先按右端点排序,然后依次遍历,能坐车的就让它们坐车,这样一定是最优的。
在贪心的时候,我们要知道在车在当前的时间段中最少有多少空位,可以用线段树维护(也可以不用线段树,但个人感觉用线段树比较好理解)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls p<<1
#define rs p<<1|1
#define mid ((l+r)>>1)
using namespace std;
int read(){
    int k=0; char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9')
      k=k*10+c-48,c=getchar();
    return k;
}
struct zzz{
    int st,en,num;
}cow[50010];
bool cmp(zzz x,zzz y){ //按右端点排序
    return x.en < y.en;
}
int tree[20010<<2],tag[20010<<2];  //以下为线段树维护区间最大值
inline void down(int l,int r,int p){
    tree[ls]+=tag[p]; tree[rs]+=tag[p];
    tag[ls]+=tag[p]; tag[rs]+=tag[p];
    tag[p]=0;
}
inline void up(int p){
    tree[p]=max(tree[ls],tree[rs]);
}
void update(int l,int r,int p,int nl,int nr,int k){
    if(l>=nl&&r<=nr){
        tree[p]+=k; tag[p]+=k; return ;
    }
    down(l,r,p);
    if(nl<=mid) update(l,mid,ls,nl,nr,k);
    if(nr>mid) update(mid+1,r,rs,nl,nr,k);
    up(p);
}
int query(int l,int r,int p,int nl,int nr){
    int ans=0;
    if(l>=nl&&r<=nr) return tree[p];
    down(l,r,p);
    if(nl<=mid) ans=max(ans,query(l,mid,ls,nl,nr));
    if(nr>mid) ans=max(ans,query(mid+1,r,rs,nl,nr));
    return ans;
}
int main(){
    int k=read(),n=read(),c=read();
    for(int i=1;i<=k;i++)
      cow[i].st=read(),cow[i].en=read()-1,cow[i].num=read();
    sort(cow+1,cow+k+1,cmp);
    int maxn=0;
    for(int i=1;i<=k;i++){  //遍历区间
        int x=query(1,n,1,cow[i].st,cow[i].en);
        if(x<c){
            int f=min(c-x,cow[i].num);  //当前能有几头奶牛上车
            maxn+=f;
            update(1,n,1,cow[i].st,cow[i].en,f);
        }
    }
    cout<<maxn;
    return 0;
}

USACO09FEB Fair Shuttle的更多相关文章

  1. 洛谷 P1607 [USACO09FEB]庙会班车Fair Shuttle 解题报告

    P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...

  2. 洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle

    P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...

  3. 【贪心】洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle 题解

        不是很容易写出正解的贪心问题. 题目描述 Although Farmer John has no problems walking around the fair to collect pri ...

  4. P1607 [USACO09FEB]庙会班车Fair Shuttle

    题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...

  5. [USACO09FEB]庙会班车Fair Shuttle 线段树维护maxx&&贪心

    题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...

  6. 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树

    Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...

  7. [USACO09FEB]庙会班车Fair Shuttle

    题目描述 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车 ...

  8. 线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle

    Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼--如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛 ...

  9. <USACO09FEB>庙会捷运Fair Shuttleの思路

    一个没有被我成功证明的 贪心 但是 ac了的 别人排序都是排终点.但我的排终点错了emm排起点才对qvq 有没有人友情看看怎么证(没有 #include<cstdio> #include& ...

随机推荐

  1. 《算法竞赛进阶指南》1.4Hash

    137. 雪花雪花雪花 有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,-,ai,6. 因为雪花的形状是封闭的环形,所以从任何一 ...

  2. 兼容IE的login表单巧妙写法

    利用label来写: HTML: <div class="loginwrap"> <label for="phonenumber" class ...

  3. java中存在垃圾回收机制,但是还会有内存泄漏的问题,原因是

    答案是肯定的,但不能拿这一句回答面试官的问题.分析:JAVA是支持垃圾回收机制的,在这样的一个背景下,内存泄露又被称为“无意识的对象保持”.如果一个对象引用被无意识地保留下来,那么垃圾回收器不仅不会处 ...

  4. java编程如何实现多条2017-08-08 22:10:00.0这样的时间数据,相差多少天?(隔24小时为相差1天,否则为0天)

    不多说,直接上干货! 这是yyyy-MM-dd HH:mm:ss.S     GetIntervalDays.java package zhouls.bigdata.DataFeatureSelect ...

  5. 几种复杂度的斐波那契数列的Java实现

    一:斐波那契数列问题的起源 13世纪初期,意大利数论家Leonardo Fibonacci在他的著作Liber Abaci中提出了兔子的繁殖问题: 如果一开始有一对刚出生的兔子,兔子的长大需要一个月, ...

  6. php面试题分享

    1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...

  7. Maven的学习资料收集--(一)环境搭建

    这几天在做项目的时候用到了maven,但是自己没有从来没有接触过,所以咋网上找资料,终于找到了一下的资料,这个是别人总结的,我只是转载过来积累.请尊重原创. 官网地址:http://maven.apa ...

  8. NopI 导出数据

    protected void exportAward(DataSet dsResult) { if (dsResult != null) { string fileName = System.Web. ...

  9. iOS开发ReactiveCocoa学习笔记(二)

    RAC 中常见的宏: 使用宏定义要单独导入 #import <RACEXTScope.h> 一. RAC(TARGET, [KEYPATH, [NIL_VALUE]]):用于给某个对象的某 ...

  10. linux解压与参数介绍

    linux下 各种解压文件使用方法:https://www.jianshu.com/p/ca41f32420d6 解压参数详解:http://www.cnblogs.com/jyaray/archiv ...