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

我真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实现打开Windows控制台窗口

    在写Python程序的时候突发奇想了一下,能不能用java代码实现打开控制台窗口呢? 经过查询网络资料和java API文档,终于实现了: package com.primeton.cmd; impo ...

  2. MySQL检查死锁简介

  3. ThreadLocal遇到线程池时, 各线程间的数据会互相干扰, 串来串去

    最近遇到一个比较隐蔽而又简单地问题,在使用ThreadLocal时发现出现多个线程中值串来串去,排查一番,确定问题为线程池的问题,线程池中的线程是会重复利用的,而ThreadLocal是用线程来做Ke ...

  4. c#将本地文件上传至服务器(内网)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. HDU 1398 Square Coins 平方硬币 (普通母函数,水)

    题意: 有17种硬币,每种的面值为编号的平方,比如 1,4,9,16.....给出一个数字,求组成这个面值有多少种组法? 思路: 用普通母函数解,主要做的就是模拟乘法,因为硬币是无限的,所以每个构造式 ...

  6. X11/Xlib.h: No such file or directory

    CentOS 编译一些开源项目提示:X11/Xlib.h: No such file or directory. 运行命令:yum install libX11-devel就可以了.

  7. Cannot fetch index base URL https://pypi.python.org/pypi/ 解决方法

    vi /etc/resolv.conf # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # ...

  8. UVA - 1252 Twenty Questions (状压dp)

    状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...

  9. 剑指offer42 左旋转字符串

    自己想的一个新的写法,如果不排除length=0的情况,下面那个while是死循环 class Solution { public: string LeftRotateString(string st ...

  10. navicat 常用快捷键

    1.ctrl+q           打开查询窗口 2.ctrl+/            注释sql语句3.ctrl+shift +/  解除注释4.ctrl+r           运行查询窗口的 ...