[USACO09FEB]庙会班车Fair Shuttle 线段树维护maxx&&贪心
题目描述
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his cows are not in such good shape; a full day of walking around the fair leaves them exhausted. To help them enjoy the fair, FJ has arranged for a shuttle truck to take the cows from place to place in the fairgrounds.
FJ couldn't afford a really great shuttle, so the shuttle he rented traverses its route only once (!) and makes N (1 <= N <= 20,000) stops (conveniently numbered 1..N) along its path. A total of K (1 <= K <= 50,000) groups of cows conveniently numbered 1..K wish to use the shuttle, each of the M_i (1 <= M_i <= N) cows in group i wanting to ride from one stop S_i (1 <= S_i < E_i) to another stop E_i (S_i < E_i <= N) farther along the route.
The shuttle might not be able to pick up an entire group of cows (since it has limited capacity) but can pick up partial groups as appropriate.
Given the capacity C (1 <= C <= 100) of the shuttle truck and the descriptions of the groups of cows that want to visit various sites at the fair, determine the maximum number of cows that can ride the shuttle during the fair.
逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的。所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车代步。但是,约翰木有钱,他租来的班车只能在集市上沿直线跑一次,而且只能停靠N(1 ≤N≤20000)个地点(所有地点都以1到N之间的一个数字来表示)。现在奶牛们分成K(1≤K≤50000)个小组,第i 组有Mi(1 ≤Mi≤N)头奶牛,他们希望从Si跑到Ti(1 ≤Si<Ti≤N)。
由于班车容量有限,可能载不下所有想乘车的奶牛们,此时也允许小里的一部分奶牛分开乘坐班车。约翰经过调查得知班车的容量是C(1≤C≤100),请你帮助约翰计划一个尽可能满足更多奶牛愿望的方案。
输入输出格式
输入格式:
【输入】
第一行:包括三个整数:K,N和C,彼此用空格隔开。
第二行到K+1行:在第i+1行,将会告诉你第i组奶牛的信息:Si,Ei和Mi,彼
此用空格隔开。
输出格式:
【输出】
第一行:可以坐班车的奶牛的最大头数。
输入输出样例
说明
【样例说明】
班车可以把2头奶牛从1送到5,3头奶牛从5送到8,2头奶牛从8送到14,1头
奶牛从9送到12,1头奶牛从13送到14,1头奶牛从14送到15。
贪心按照右端点排序;
然后线段树维护区间最大值;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 900005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
struct node {
int l, r;
int maxx;
int lazy;
}tree[maxn];
int n, m, k;
struct Nd {
int lft, rgt, val;
}qu[maxn];
bool cmp(Nd a, Nd b) {
if (a.rgt != b.rgt)return a.rgt < b.rgt;
return a.lft < b.lft;
}
void pushup(int rt) {
tree[rt].maxx = max(tree[rt << 1].maxx, tree[rt << 1 | 1].maxx);
}
void pushdown(int rt) {
if (tree[rt].lazy) {
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt << 1].maxx += tree[rt].lazy;
tree[rt << 1 | 1].maxx += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
void upd(int L, int R, int l, int r, int rt,int val) {
if (L <= l && r <= R) {
tree[rt].lazy += val; tree[rt].maxx += val;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid)upd(L, R, l, mid, rt << 1, val);
if (mid < R)upd(L, R, mid + 1, r, rt << 1 | 1, val);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R)return tree[rt].maxx;
pushdown(rt);
int mid = (l + r) >> 1;
int ans = -inf;
if (L <= mid)ans = max(ans, query(L, R, l, mid, rt << 1));
if (mid < R)ans = max(ans, query(L, R, mid + 1, r, rt << 1 | 1));
return ans;
} int main() {
//ios::sync_with_stdio(0);
rdint(k); rdint(n); rdint(m);
for (int i = 1; i <= k; i++) {
rdint(qu[i].lft); rdint(qu[i].rgt);
rdint(qu[i].val);
}
sort(qu + 1, qu + 1 + k, cmp);
int res = 0;
for (int i = 1; i <= k; i++) {
int tmp = query(qu[i].lft, qu[i].rgt-1, 1, n, 1);
int minn = min(m - tmp, qu[i].val);
res += minn;
upd(qu[i].lft, qu[i].rgt-1, 1, n, 1, minn);
}
cout << res << endl;
return 0;
}
[USACO09FEB]庙会班车Fair Shuttle 线段树维护maxx&&贪心的更多相关文章
- 洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle
P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...
- 洛谷 P1607 [USACO09FEB]庙会班车Fair Shuttle 解题报告
P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...
- 线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle
Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼--如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛 ...
- P1607 [USACO09FEB]庙会班车Fair Shuttle
题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...
- [USACO09FEB]庙会班车Fair Shuttle
题目描述 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车 ...
- 【贪心】洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle 题解
不是很容易写出正解的贪心问题. 题目描述 Although Farmer John has no problems walking around the fair to collect pri ...
- 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...
- 【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)
我不会做贪心题啊……贪心题啊……题啊……啊…… 我真TM菜爆了啊…… 这题就像凌乱的yyy一样,把终点排序,终点相同的按起点排序.然后维护一个查询最大值的线段树.对于一个区间[l,r],如果这个区间已 ...
- BZOJ 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 线段树 + 贪心
escription 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1&l ...
随机推荐
- html实现摇一摇并震动
今天收到了领导的通知,要APP和触屏完成摇一摇的功能,本来通过IOS调用原生还是蛮方便的,但想如果采用web前端的方式应该是实现不了的,马上就开始推脱了.可惜领导告知微信京东购物,不仅仅有摇一摇还有震 ...
- 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】
提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...
- 继续学习:C语言关键字
auto :声明自动变量 break:跳出当前循环 case:开关语句分支 char :声明字符型变量或函数 const :声明只读变量 continue:结束当前循环,开始下一轮循环 default ...
- 白话算法(6) 散列表(Hash Table) 从理论到实用(下)
[澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还是谈一场恋爱,都差不多要经历这么4个阶段: 1)从零开始.没有束缚的轻松感.似 ...
- mongodb 学习资料
1 入门 http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html http://www.cnblogs.com/hoo ...
- java内存模型(netty权威指南)
1.Java内存模型 Java虚拟机规范中试图定义一种java内存模型(java Memory Model,jmm)来屏蔽掉各种操作系统.虚拟机实现厂商和硬件的内存访问差异,以确保Java程序在所有操 ...
- Java常见设计模式之代理模式
指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其它相关业务的处理.比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与 ...
- Hibernate---Hql查询2---
hibernate.cfg.xml配置: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configurati ...
- MS SQL 取分组后的几条数据
SELECT uploaddate ,ptnumber ,instcount FROM ( SELECT ROW_NUMBER() OVER( PARTITION BY uploaddate ORDE ...
- File 类 操作实例
File 操作 <介绍> 尽管java.io定义的大多数类是实行流式操作的,File类不是.它直接处理文件和文件系统.也就是说,File类没有指定信息怎样从文件读取或向文件存储:它描述了文 ...