这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便

先按起始时间从小到大排序。

我用的是greater重定义优先队列(小根堆)。用pair存牛棚用完时间(first)和牛棚编号(second),每次查看队首的first是否比当前牛的起始时间早,是则弹出队首记录当前牛的答案,再把新的pair放进去,否则增加牛棚,同样要塞进队里

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=200005;
int n,ans,bl[N];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
struct qwe
{
int l,r,id;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i].l=read(),a[i].r=read(),a[i].id=i;
sort(a+1,a+1+n,cmp);
ans=1,bl[a[1].id]=1;
q.push(make_pair(a[1].r,1));
for(int i=2;i<=n;i++)
{
if(q.top().first<a[i].l)
{
bl[a[i].id]=q.top().second;
q.push(make_pair(a[i].r,q.top().second));
q.pop();
}
else
{
bl[a[i].id]=++ans;
q.push(make_pair(a[i].r,ans));
}
}
printf("%d\n",ans);
// for(int i=1;i<=n;i++)
// printf("%d\n",bl[i]);
return 0;
}

如果不输出方案的话,因为时间范围是1e6,所以直接差分做即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int n,ans,mx,t[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
int l=read(),r=read();
t[l]++,t[r+1]--;
mx=max(mx,r);
}
for(int i=1;i<=mx;i++)
ans=max(ans,t[i]+=t[i-1]);
printf("%d\n",ans);
return 0;
}

bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】的更多相关文章

  1. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

    线段树.. -------------------------------------------------------------------------------------- #includ ...

  2. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...

  3. BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚:优先队列【线段最大重叠层数】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1651 题意: 给你n个线段[a,b],问你这些线段重叠最多的地方有几层. 题解: 先将线段 ...

  4. 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 566  Sol ...

  5. 【BZOJ】1651: [Usaco2006 Feb]Stall Reservations 专用牛棚(线段树/前缀和 + 差分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1651 很奇妙.. 我们发现,每一时刻的重叠数选最大的就是答案.... orz 那么我们可以线段树维护 ...

  6. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

  7. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考

    Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...

  8. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

    dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边 ...

  9. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows

    题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 234  Solve ...

随机推荐

  1. maven运行tomcat6出现错误Exception starting filter encodingFilter怎么解决

    严重: Exception starting filter encodingFilterjava.lang.ClassCastException: org.springframework.web.fi ...

  2. UI & 原型

    UI & 原型 产品原型设计与团队实时协作平台 https://www.xiaopiu.com/download https://www.xiaopiu.com/forum/posts/5b9 ...

  3. HDU1755

    这道题直接暴力枚举复杂度为 n!*m 但是k<100 , 所以我们可以通过取模用dp[i][j] 表示k=i 时,-x取模k为j的最小值 #include <cstdio> #inc ...

  4. 【进击后端】mongodb入门

    环境:ubuntu 重要字:数据库,集合,记录,字段 api地址:https://docs.mongodb.com/manual/reference/method/db.collection.upda ...

  5. Oracle SqlPlus导出查询结果

    Oracle SqlPlus导出查询结果 在sqlplus下导出查询的结果保存到本地sql文件中,可以采用如下方式:1.连接数据库: sqlplus xmq/xmqpwd@192.168.1.57:1 ...

  6. pycharm内存不足时如何修改设置?

    Help->Find Action->(type "VM Options")->(Click)"Edit Custom VM Options" ...

  7. 手把手教你编写一个简单的PHP模块形态的后门

    看到Freebuf 小编发表的用这个隐藏于PHP模块中的rootkit,就能持久接管服务器文章,很感兴趣,苦无作者没留下PoC,自己研究一番,有了此文 0×00. 引言 PHP是一个非常流行的web ...

  8. javascript 时间格式(很方便的原生函数)

    原文:http://www.cnblogs.com/yjf512/p/3796229.html --------------------- <html xmlns="http://ww ...

  9. 百度知道的代码复制粘贴到VB没有换行怎么办

    在如下所示的网页中,复制 粘贴到word文档,换行还是有的   再复制到VB6.0中还是可用的

  10. DEV Express控件VScorllBar控件使用

    今天使用VScorllBall控件做了个控制界面上下滑动的功能,网上也找了这方面的资料,大概综合借鉴了一下之后,搞了一个适合我自己的自定义功能控件. 下面话不多说,直接上代码. private voi ...