题目传送门

据说\(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. 在pom包中添加spring-boot-starter-test包引用

    有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...

  2. Gson本地和服务器环境不同遇到的Date转换问题 Failed to parse date []: Invalid time zone indicator

    GoogleGson在处理Date格式时有个小陷阱,在不同环境中部署时可能会遇到问题. Gson默认处理Date对象的序列化/反序列化是通过一个SimpleDateFormat对象来实现的,通过下面的 ...

  3. dorado开发模式下实现动态查询

    使用dorado开发模式,我们可以实现以下开发技巧 开发技巧1.实现动态查询功能: 1. 查询按钮的onClick事件中写入: datasetEmployee.parameters().setValu ...

  4. 模型事件注意点,before_delete、after_delete、before_write、after_write、before_update、after_update、before_insert、after_insert

    模型类支持before_delete.after_delete.before_write.after_write.before_update.after_update.before_insert.af ...

  5. bootstrap table 学习记录

    效果: html代码: <!-- 工具容器 --> <div id="toolbar" class="btn-group"> <b ...

  6. Vertical roller mill from SBM

    Vertical roller mill has many different forms, but it works basically the same. All of these forms o ...

  7. Mybatis的小计

    1连接池 一 我的错误想法 poolMaximumIdleConnections 最大活跃连接数 poolMaximumActiveConnections 最大空闲连接数 我一直以为 空闲是一直存在的 ...

  8. Spring注解之@Lazy注解,源码分析和总结

    一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...

  9. Docker for mac 安装 kong

    首先安装一个 PostgreSQL,选的版本是 9.5 $ docker run -d --name kong-database \ -p : \ -e "POSTGRES_USER=kon ...

  10. JAVA中数组介绍

    一.数组: 数组指一组数据的集合,数组中的每个数据被称作元素. 二.数组定义: 数组类型[] 数组名 = new 数组类型[元素个数或数组长度]: (注意:等号前面的[]里面不能写任何东西) 也可以以 ...