我不会做贪心题啊……贪心题啊……题啊……啊……

我真TM菜爆了啊……

这题就像凌乱的yyy一样,把终点排序,终点相同的按起点排序。然后维护一个查询最大值的线段树。对于一个区间[l,r],如果这个区间已经有的最大值为s,那么这个区间最多还能装下c-s头奶牛。

当然奶牛数量没那么多的话我也是没有办法

最后说一句,奶牛到终点就下车了,可以给别的奶牛腾空间,不计入个数。所以奶牛在车上的区间为[l,r-1]。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 100001
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
#define root 1, 1, n
#define ls now << 1, l, mid
#define rs now << 1 | 1, mid + 1, r int k, n, c, ans;
int sum[N << ], add[N << ]; struct node
{
int s, t, m;
}p[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool cmp(node x, node y)
{
return x.t < y.t;
} inline void pushdown(int now)
{
if(add[now])
{
sum[now << ] += add[now];
sum[now << | ] += add[now];
add[now << ] += add[now];
add[now << | ] += add[now];
add[now] = ;
}
} inline int query(int now, int l, int r, int x, int y)
{
if(x <= l && r <= y) return sum[now];
pushdown(now);
int mid = (l + r) >> , ret = ;
if(x <= mid) ret = max(ret, query(ls, x, y));
if(mid < y) ret = max(ret, query(rs, x, y));
return ret;
} inline void update(int now, int l, int r, int x, int y, int d)
{
if(x <= l && r <= y)
{
sum[now] += d;
add[now] += d;
return;
}
pushdown(now);
int mid = (l + r) >> ;
if(x <= mid) update(ls, x, y, d);
if(mid < y) update(rs, x, y, d);
sum[now] = max(sum[now << ], sum[now << | ]);
} int main()
{
int i, x;
k = read();
n = read();
c = read();
for(i = ; i <= k; i++)
{
p[i].s = read();
p[i].t = read();
p[i].m = read();
}
std::sort(p + , p + k + , cmp);
for(i = ; i <= k; i++)
{
x = query(root, p[i].s, p[i].t - );
if(x < c)
{
update(root, p[i].s, p[i].t - , min(p[i].m, c - x));
ans += min(p[i].m, c - x);
}
}
printf("%d\n", ans);
return ;
}

【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)的更多相关文章

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

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

  2. BZOJ 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 线段树 + 贪心

    escription 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1&l ...

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

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

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

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

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

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

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

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

  7. Luogu P1607 庙会班车【线段树】By cellur925

    题目传送门 据说可以用贪心做?算了算了...我都不会贪.... 开始想的是用线段树,先建出一颗空树,然后输进区间操作后就维护最大值,显然开始我忽视了班车的容量以及可以有多组奶牛坐在一起的信息. 我们肯 ...

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

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

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

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

随机推荐

  1. Java之instanceof

    class Base{     int x = 1;     static int y = 2;     String name(){         return "mother" ...

  2. Git服务器和Git权限管理应用GITLAB安装方法

    首先声明,本文使用的服务器是Centos 6.5,在其他版本的LINUX上运行不保证也是一样的效果. 顺便说下 来波点赞 来波收藏和推荐  有什么问题 我会一直关注评论的 想放一张最终图吧 其中主要涉 ...

  3. uiviewcontroller 键盘不遮挡信息

    //添加监听事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow: ...

  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource

    spring boot web项目运行时提示如下错误 org.springframework.beans.factory.BeanCreationException: Error creating b ...

  5. Words Prefixed Trans-

    transit v. Pass across or through (an area) The new large ships will be too big to transit the Panam ...

  6. EF+linq的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  7. Tensorflow_入门学习_2_一个神经网络栗子

    3.0 A Neural Network Example 载入数据: from tensorflow.examples.tutorials.mnist import input_data mnist ...

  8. rf统计条数

    js模式 直接引用关键字模式

  9. linux设置http/https proxy及忽略proxy的方法

    msys2设置网络代理 在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="19 ...

  10. JdbcTemplate类对sql的操作使用

    <!--方式一: dbcp 数据源配置,在测试环境使用单连接 --> <bean id="dataSource" class="org.apache.c ...