题目传送门

据说\(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. idea长用快捷键

    1CA(ctrl+alt) 1.1(记忆mlv) ctrl alt m 代码封装成方法 ctrl alt L格式化代码 ctrl alt v补全返回值 (eclipse: alt shift l) 1 ...

  2. php微信公众号开发简单记录

    开发前准备:1.服务器 2.微信公众号测试号(有真实的账号更好) 测试号申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/lo ...

  3. 旅行青蛙分析(Android篇)

    近期旅行青蛙这款游戏非常的火热,周围的朋友.家人都养了一只小青蛙.看到网上有人说这款游戏可以直接逆向编译,没有加密:所以在搜索相关资料后花了一些时间进行逆向分析与修改.这篇文章里,我将介绍如何获取稀有 ...

  4. 3、python数据类型之字符串(str)

    字符串是任何计算机语言中最常见的数据类型,python中也不例外,因此熟练掌握字符串是学习python的必备技能.python中字符串三种定义方式 str = 'wang' str = "w ...

  5. LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数

    Given an array of integers, every element appears twice except for one. Find that single one. class ...

  6. OpenStack Cirros

    Cirros Openstack的开发,基本都使用这个image来测试,因为他比较小,只有10M. 镜像介绍 镜像的地址: https://launchpad.net/cirros/trunk/0.3 ...

  7. 【转】《Unity Shader入门精要》冯乐乐著 书中彩图

    为方便个人手机学习时候查阅,从网上转来这些彩图. 如属过当行为,联系本人删除. 勘错表 http://candycat1992.github.io/unity_shaders_book/unity_s ...

  8. Java继承改进

    一.java继承改进 首先,多继承的缺点: 1.继承多个父类,父类中方法名相同,产生歧义 2.父类中方法同名,子类未覆盖,也会歧义 所以,java改进,类只能单继承,接口可以多继承 接口中只有抽象方法 ...

  9. arcgis js 几种拓扑关系详解

    arcgis js的拓扑关系,在处理复杂逻辑和分析时,可以通过拓扑关系,减小客户端的工作量 拓扑关系: 1.overlaps 重叠 这里的重叠跟平时我们理解的不太一样,这里的重叠,必须是A与B有交集, ...

  10. Day1下午

    T1 暴力50分 排A和B X,不用考虑X    用数组80分, 权值线段树.平衡树100, 一个函数? T2 打表  dp logn+1,+ 搜索,dp? txt..... T3 30分暴力和尽量均 ...