USACO09FEB Fair Shuttle
据说\(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的更多相关文章
- 洛谷 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 题解
不是很容易写出正解的贪心问题. 题目描述 Although Farmer John has no problems walking around the fair to collect pri ...
- P1607 [USACO09FEB]庙会班车Fair Shuttle
题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...
- [USACO09FEB]庙会班车Fair Shuttle 线段树维护maxx&&贪心
题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...
- 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...
- [USACO09FEB]庙会班车Fair Shuttle
题目描述 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车 ...
- 线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle
Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼--如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛 ...
- <USACO09FEB>庙会捷运Fair Shuttleの思路
一个没有被我成功证明的 贪心 但是 ac了的 别人排序都是排终点.但我的排终点错了emm排起点才对qvq 有没有人友情看看怎么证(没有 #include<cstdio> #include& ...
随机推荐
- any cpu ×86 ×64
On a 32-bit machine: Any CPU: runs as a 32-bit process, can load Any CPU and x86 assemblies, will ge ...
- Linux调优(内存,CPU)
一.相关概念简介 system call:系统调用 time slice:cpu时间片 O(1):Linux系统进程调度器 page frame:分页 RSS:常驻内存集,无法被页面化的数据 MMU: ...
- 无法访问http,会强制跳到https
1. Close Safari. 2. Delete the ~/Library/Cookies/HSTS.plist file. 3. Reopen Safari.
- Jmeter report优化
优化大致过程 生成并的报告模板: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet ...
- HTML 常用头部标签(meta)
先来看下常用的标签列表,后文会一一介绍: <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang=&quo ...
- 前端 HTML-CSS 规范
黄金定律 一个项目应该永远遵循同一套编码规范! 不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的. HTML 语法 用两个空格来代替制表符(tab) – 这是唯一能保证在所有环境 ...
- 手动添加git 到 右键菜单
1.通过在“运行”中输入‘regedit’,打开注册表. 2.找到[HKEY_CLASSES_ROOT\Directory\Background]. 3.在[Background]下如果没有[shel ...
- 纯干货:Linux抓包命令集锦
/******************************************************************************************* 版权声明* 本 ...
- HDU 5785 Interesting manacher + 延迟标记
题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2 ...
- C# 枚举类型 enum (一)
1.枚举使用enum关键字来声明,与类同级.枚举可以和类并列也可以 写在类里面,不能写在方法里. 2.枚举是值类型,隐式继承自System.Enum,不能手动修改. System.Enum本身是引用类 ...