题目传送门

据说\(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. 关于Git的一些操作记录

    本文是我的一些记录,不按教学顺序 1.如何添加不上传的文件或文件夹,并且将已经添加到远程库的文件夹删除 操作过程如下: vim .gitignore // 按i进入编辑模式 写入 node_modul ...

  2. windows下运行jar

    run.bat 1. javaw运行 @echo offstart javaw -Xmx128m -Xms64m -jar testlog.jarexit 2.java运行 @echo offjava ...

  3. postgresql导出某张表的数据

    \copy 表名 to 路径 with csv 比如: \copy dataset to /home/backup/dataset.csv with csv \copy dataset to /hom ...

  4. POJ 1061青蛙的约会。求解(x+mT)%L=(y+nT)%L的最小步数T。

    因为是同余,所以就是(x+mT)%L-(y+nT)%L=0.可以写成(x-y+(m-n)T)%L=0.就是这个数是L的倍数啦.那么我可以这样x-y+(m-n)T + Ls = 0.就可以了,s可正可负 ...

  5. (6)ASP.NET Core 中使用IHttpClientFactory发出HTTP请求

    1.HttpClient类使用存在的问题 HttpClient类的使用所存在的问题,百度搜索的文章一大堆,好多都是单纯文字描述,让人感觉不太好理解,为了更好理解HttpClient使用存在的问题,下面 ...

  6. Java排序算法(一)

    Java排序算法(一) 排序的基本概念和分类 1.1排序的定义 在<大话数据结构>中,排序定义为,假设含有n个记录的序列为{r1,r2,...,rn},其相应的关键字{k1,k2,..., ...

  7. electron 开发记录

    判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...

  8. ruby firefox23报错:waiting for evaluate.js load failed

    解决方法 gem install selenium-webdriver -v='2.34.0'

  9. 【心得】asp.net 异常:正在中止线程 引发的问题

    asp.net做的一个同步程序,同步的方法是通过JQuery的Ajax调用,同步过程所需要的时间大概有几个小时吧. 当在本机运行的时候,无论是debug模式还是release模式,都能正常运行. 发布 ...

  10. jmeter压测配置

    windows上面修改最大使用端口数和time_await等待时间 注册表需要添加两个配置,位置:HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ S ...