题目描述

逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的。所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车代步。但是,约翰木有钱,他租来的班车只能在集市上沿直线跑一次,而且只能停靠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,彼

此用空格隔开。

输出格式:

【输出】

第一行:可以坐班车的奶牛的最大头数。

输入输出样例

输入样例#1:

8 15 3
1 5 2
13 14 1
5 8 3
8 14 2
14 15 1
9 12 1
12 15 2
4 6 1
输出样例#1:

10

说明

【样例说明】

班车可以把2头奶牛从1送到5,3头奶牛从5送到8,2头奶牛从8送到14,1头

奶牛从9送到12,1头奶牛从13送到14,1头奶牛从14送到15。

题解:

线段树

根据贪心思想,按结束点从小到大排序,对于每组牛,只要满足加入x只后区间最大值小于c即可。

用线段树维护区间最大值,区间加,记得修改和询问时终点要减一(终点已下车)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int s,t,m;
}a[];
int lazy[],c1[],k,n,c,ans;
bool cmp(Node a,Node b)
{
return (a.t<b.t||(a.t==b.t&&a.s>b.s));
}
void pushdown(int rt)
{
if (lazy[rt])
{
c1[rt*]+=lazy[rt];
lazy[rt*]+=lazy[rt];
c1[rt*+]+=lazy[rt];
lazy[rt*+]+=lazy[rt];
lazy[rt]=;
}
}
int query(int rt,int l,int r,int L,int R)
{
if (l>=L&&r<=R)
{
return c1[rt];
}
pushdown(rt);
int mid=(l+r)/;
int s=;
if (L<=mid) s=max(s,query(rt*,l,mid,L,R));
if (R>mid) s=max(s,query(rt*+,mid+,r,L,R));
return s;
}
void add(int rt,int l,int r,int L,int R,int d)
{
if (l>=L&&r<=R)
{
c1[rt]+=d;
lazy[rt]+=d;
return;
}
pushdown(rt);
int mid=(l+r)/;
if (L<=mid) add(rt*,l,mid,L,R,d);
if (R>mid) add(rt*+,mid+,r,L,R,d);
c1[rt]=max(c1[rt*],c1[rt*+]);
}
int main()
{int i,j;
cin>>k>>n>>c;
for (i=;i<=k;i++)
{
scanf("%d%d%d",&a[i].s,&a[i].t,&a[i].m);
a[i].t--;
}
sort(a+,a+k+,cmp);
for (i=;i<=k;i++)
{
int x=min(a[i].m,c-query(,,n,a[i].s,a[i].t));
add(,,n,a[i].s,a[i].t,x);
ans+=x;
}
cout<<ans;
}

[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. 线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle

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

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

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

  8. 【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)

    我不会做贪心题啊……贪心题啊……题啊……啊…… 我真TM菜爆了啊…… 这题就像凌乱的yyy一样,把终点排序,终点相同的按起点排序.然后维护一个查询最大值的线段树.对于一个区间[l,r],如果这个区间已 ...

  9. <USACO09FEB>庙会捷运Fair Shuttleの思路

    一个没有被我成功证明的 贪心 但是 ac了的 别人排序都是排终点.但我的排终点错了emm排起点才对qvq 有没有人友情看看怎么证(没有 #include<cstdio> #include& ...

随机推荐

  1. python 进程复习

    import os import time ret = os.fork() # 创建子线程 if ret ==0: # 子进程中返回值为0,父进程>0 while True: print('.. ...

  2. tornado web高级开发项目

    抽屉官网:http://dig.chouti.com/ 一.配置(settings) settings = { 'template_path': 'views', #模板文件路径 'static_pa ...

  3. HTML标签小记文本类标签

    文本类标签: <input type="text" name="" value="">文本框  type(方式,方法)name文 ...

  4. python之路--day15--常用模块之logging模块

    常用模块 1 logging模块 日志级别:Noset (不设置) Debug---(调试信息)----也可用10表示 Info--(消息信息)----也可用20表示 Warning---(警告信息) ...

  5. JAVA_SE基础——24.面向对象的内存分析

    黑马程序员入学blog ... 接着上一章的代码: //车类 class Car{ //事物的公共属性使用成员变量描述. String name; //名字的属性 String color; //颜色 ...

  6. python time、datetime、random、os、sys模块

    一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织 ...

  7. gdb-peda调试总汇

    gdb-peda调试总汇 break *0x400100 (b main):在 0x400100 处下断点 tb一次性断点 info b:查看断点信息 delete [number]:删除断点 wat ...

  8. restful架构风格设计准则(三)资源识别和资源设计

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! restful风格的设计中,首先要识别系统中的资源,然后用HTTP规范表 ...

  9. python 爬取百度翻译进行中英互译

    感谢RoyFans 他的博客地址http://www.cnblogs.com/royfans/p/7417914.html import requests def py(): url = 'http: ...

  10. mysql Access denied for user root@localhost错误解决方法

    select * from user \G use mysql select * from user limit 1 \G update user set Host='%' where `User`= ...