bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】
这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便
先按起始时间从小到大排序。
我用的是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 专用牛棚【贪心+堆||差分】的更多相关文章
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )
线段树.. -------------------------------------------------------------------------------------- #includ ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 553 ...
- BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚:优先队列【线段最大重叠层数】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1651 题意: 给你n个线段[a,b],问你这些线段重叠最多的地方有几层. 题解: 先将线段 ...
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 566 Sol ...
- 【BZOJ】1651: [Usaco2006 Feb]Stall Reservations 专用牛棚(线段树/前缀和 + 差分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1651 很奇妙.. 我们发现,每一时刻的重叠数选最大的就是答案.... orz 那么我们可以线段树维护 ...
- BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 509 Sol ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考
Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...
- 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 ) ) 边 ...
- 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 ...
随机推荐
- hdu 2433 Travel (最短路树)
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...
- DEA中MAVEN项目有多个子目录,如何加载构建
ddts这个项目有三个子目录,每个子目录下面也都有一个 pom.xml 此时需要 右键子目录的 pom.xml,选择Add as Maven Project,在上图中cli.core两个目 ...
- 乱记结论之OI常用四大数列
一.斐波那契数列 $f(0)=1,f(1)=1,f(i)=f(i-1)+f(i-2) \ \ \ \ (i>=2)$ 经典的解释是兔子生小孩,第0年一对兔子,一对兔子需要一年长大,后面每年都生小 ...
- 洛谷——P1036 选数
题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ...
- operamasks—omBorderLayout布局
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Eclipse编辑YAML插件-YEdit
官网:https://github.com/oyse/yedit 离线版本:(链接: https://pan.baidu.com/s/1b1j2gQ 密码: wyyb) 安装方法:直接复制JAR包到P ...
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
Jedis是Redis的Java客户端,Spring将Jedis连接池作为一个Bean来配置.如果在Spring Data的官网上可以发现,Spring Data Redis已经将Jedis集成进去了 ...
- How to force immediate stop of threads in Jmeter servers如何在jmeter执行完,立即停止jmeter
https://stackoverflow.com/questions/38900315/how-to-force-immediate-stop-of-threads-in-jmeter-server ...
- centos7容量扩充
新买的2T 绿盘到货了~~好开心的说~但毕竟是第一次安装,事先还是在网上搜索了很多资料才敢动手,下面就开始啦~ 环境:Centos7.dell服务器.2T容量绿盘 1.硬盘连接好之后,开机先使用fdi ...
- Tree Operations 打印出有向图中的环
题目: You are given a binary tree with unique integer values on each node. However, the child pointers ...