POJ-3190 Stall Reservations---优先队列+贪心
题目链接:
https://vjudge.net/problem/POJ-3190
题目大意:
有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶。挤奶的时候奶牛一定要单独放在一个牛棚中。一头奶牛的结束时间与另一头奶牛的开始时间重合的时候2头奶牛不能放在同一个牛棚中,例A牛时间[5,10],B牛时间[10,12],那么需要用2个牛棚放这2头牛。输入N头牛的所有的挤奶时间区间,要求输出最少要建几个牛棚,以及每头牛挤奶的时候在第几号牛棚。
思路:
将奶牛根据挤奶开始时间排序后,从第一只奶牛进行考虑。从牛棚的优先队列中返回结束时间最短的牛棚,看该奶牛是否能放在该牛棚中。如果能,就更新该牛棚的结束时间,更新为该奶牛的开始时间;如果不能,就新创建一个牛棚,并将奶牛放在这个牛棚中。重复以上,直到所有奶牛都被放完。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int> Pair ;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ;
int T, n, m, cases;
struct node
{
int x, y, id;
bool operator <(const node a)const
{
return y > a.y;
}
};
bool cmp(node a, node b)
{
return a.x < b.x || a.x == b.x && a.y < b.y;
}
node a[maxn];
int id[maxn];
int main()
{
cin >> n;
for(int i = ; i <= n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
a[i].id = i;
}
sort(a + , a + n + , cmp);
priority_queue<node>q;
int ans = ;
id[a[].id] = ;
q.push(a[]);
for(int i = ; i <= n; i++)
{
node now = q.top();
if(a[i].x > now.y)
{
q.pop();
id[a[i].id] = id[now.id];
q.push(a[i]);
}
else
{
id[a[i].id] = ++ans;
q.push(a[i]);
}
}
cout<<ans<<endl;
for(int i = ; i <= n; i++)cout<<id[i]<<endl;
return ;
}
POJ-3190 Stall Reservations---优先队列+贪心的更多相关文章
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- sqlserver 常用语法
sqlserver查找 table, view, column select * from information_schema.tables where table_schema='bk' sele ...
- python3 zip()函数笔记
a=[1,2,3]b=[4,5,6] for A ,B in zip(a,b):#用zip()函数整体打包 print(A,B)
- Oracle trunc函数使用
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(trunc(sysdate), 'yyyy-mm-dd hh24:mi:ss') f ...
- Oozie的缺点
Oozie使用的时候有以下不便: [a]Oozie调度的Workflow只能使用XML文件配置 [b]启动调度只能通过命令行 [c]无法通过Oozie界面调试调度脚本 [d]Oozie无法可视化调试脚 ...
- RequestContextHolder与RequestContextUtils
org.springframework.web.servlet.support.RequestContextUtils 在spring-webmvc中, 主要用来获取WebApplicationCon ...
- my16_sql_thread执行慢导致主从延迟高的一个情景
现象:从库延迟高,查看slave status发现sql_thread执行语句的速度比主库慢,这样的延迟会一直高下去,下面是排查的一些过程1. 检查了从库的配置,磁盘的写入速度的确没有主库高2. io ...
- Message Unable to connect to SQL Server '(local)'
最近在sql server 加了一些job,但是run job的时候发生了一下错误: ssage Unable to connect to SQL Server '(local)' 问题根源:调用 T ...
- java 开发体系参考学习
https://www.oschina.net/question/2663968_2283797
- CentOS7/Ubuntu Linux java jdk 环境变量配置
[root@localhost sony]# tar -zxvf jdk-8u121-linux-x64.tar.gz [root@localhost sony]# .0_121 /usr/local ...
- Murano Weekly Meeting 2015.08.11
Meeting time: 2015.August.11th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting summ ...