P1607 [USACO09FEB]庙会班车Fair Shuttle

题目描述

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,彼

此用空格隔开。

输出格式:

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


这题的做法还是比较多哒

一看是区间选不选之类的那不就是排个序然后想办法贪心贪心呗云云

按左端点排序

拿一颗平衡树维护在车上的奶牛的右端点

当有左端点进来时,权值小于这颗左端点的点下车

然后上车上到车满

然后比一比右端点,把右端点大的踢出去


Code:

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
const int N=5e4+10;
int kk,n,c;//k个区间,n个时间,c的容量
struct node
{
int s,t,m;
bool friend operator <(node n1,node n2)
{
return n1.s<n2.s;
}
}gro[N];
int root,dat[N<<6],siz[N<<6],ch[N<<6][2],val[N<<6],tot;
#define ls ch[now][0]
#define rs ch[now][1]
void updata(int now)
{
siz[now]=siz[ls]+siz[rs]+1;
}
void split(int now,int k,int &x,int &y)
{
if(!now) {x=y=0;return;}
if(dat[now]<=k)
{
x=now;
split(rs,k,rs,y);
}
else
{
y=now;
split(ls,k,x,ls);
}
updata(now);
}
int Merge(int x,int y)
{
if(!x||!y) return x+y;
if(val[x]>val[y])
{
ch[x][1]=Merge(ch[x][1],y);
updata(x);
return x;
}
else
{
ch[y][0]=Merge(x,ch[y][0]);
updata(y);
return y;
}
}
int New(int k)
{
dat[++tot]=k,val[tot]=rand(),siz[tot]=1;
return tot;
}
void Insert(int k)
{
int x,y;
split(root,k,x,y);
root=Merge(x,Merge(New(k),y));
}
void extrack(int k)
{
int x,y,z;
split(root,k,x,y);
split(x,k-1,x,z);
z=Merge(ch[z][0],ch[z][1]);
root=Merge(x,Merge(z,y));
}
int mx()
{
int now=root;
while(rs) now=rs;
return dat[now];
}
int mi()
{
int now=root;
while(ls) now=ls;
return dat[now];
}
int main()
{
srand(time(0));
scanf("%d%d%d",&kk,&n,&c);
for(int i=1;i<=kk;i++)
scanf("%d%d%d",&gro[i].s,&gro[i].t,&gro[i].m);
std::sort(gro+1,gro+1+kk);
int ans=0;
for(int i=1;i<=kk;i++)
{
int mmi,mmx;
while(siz[root]&&(mmi=mi())<=gro[i].s) extrack(mmi),++ans;//下车
while(gro[i].m&&siz[root]<c) Insert(gro[i].t),--gro[i].m;//上车
while(gro[i].m&&(mmx=mx())>gro[i].t) extrack(mmx),Insert(gro[i].t),--gro[i].m;
//踢人
}
printf("%d\n",ans+siz[root]);
return 0;
}

2018.8.28

最新文章

  1. 设置这些之后,Google突然可以打开了
  2. 一个Java递归删除目录的方法
  3. django创建项目
  4. maven时候Embedded error: error in opening zip file
  5. 【自动化测试】Selenium excel操作
  6. Scala IDE for Eclipse的下载、安装和WordCount的初步使用(本地模式和集群模式)
  7. 新Azure 服务仪表盘!
  8. mod_python模块安装
  9. 搭建SpringMVC+Hibernate
  10. REdis MASTER aborted replication NOAUTH Authentication required
  11. 「Android」adb调试源码(针对dumpsys SurfceFlinger、trace.txt获取)
  12. python 全栈开发,Day18(对象之间的交互,类命名空间与对象,实例的命名空间,类的组合用法)
  13. SqlServer字符串拼接
  14. MySQL数据库的学习
  15. create-react-app:reject和不reject(使用react-app-rewired)这2种情况下的antd组件按需引入配置
  16. MySQL递归查询树状表的子节点、父节点
  17. three.js是什么,能干嘛,和webgl什么关系
  18. python传参
  19. Python OCR提取普通数字图形验证中的数字
  20. BMP格式转JPEG格式

热门文章

  1. Java源码解析——集合框架(四)——LinkedListLinkedList原码分析
  2. PHP错误:Warning: preg_replace() [function.preg-replace]: Unknown modifier &#39;[&#39; in
  3. centos配置npm全局安装
  4. 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
  5. python2.7入门---file(文件)&amp;OS 文件&amp;目录方法
  6. 利用JS调取电脑摄像头,实现拍照功能
  7. 1 opencv2.4 + vs2013
  8. Gradle 设置本地meaven
  9. python linecache读取过程
  10. 2.Linux文件和目录