【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations
原文是English,这里直接上中文吧
Descriptions:
帮助FJ做以下事:
- 使每只牛都有专属时间的最小牛棚数
- 每只牛在哪个牛棚
也许有很多可行解。输出一种即可,采用SPJ
Input
第 2..N+1行: 第 i+1行 描述了i号奶牛挤奶的起止时间
Output
Lines 2..N+1: 第 i+1行 描述了i奶牛被安排的牛棚
Sample Input
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
1
2
3
2
4
首先,把所有奶牛按挤奶开始时间从小到大排序。
然后,为第一头奶牛分配一个畜栏。
最后,依次处理后面每头奶牛i。处理i时,考虑已分配畜栏中,结束时间最早的畜栏x。如何处理详情见代码
露要用优先队列存放已经分配的畜栏,并使得结束时间最早的畜栏始丝列头部
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
struct cow//奶牛
{
int s,e;//挤奶时间的开始与结束
int no;//奶牛编号
//以挤奶时间小的规则排序
bool operator < (const cow & c) const
{
return s<c.s;
}
};
cow cows[];
int pos[];//记录每头牛在哪个畜栏
struct stall//畜栏
{
int over;//畜栏使用结束时间
int No;//畜栏编号
//以畜栏使用结束时间小的规则排序
bool operator<(const stall & c) const
{
return over>c.over;//优先队列排序相反
}
stall(int over,int No):over(over),No(No){}
};
priority_queue<stall> q;//优先队列
int main()
{
int N;
int total=;//总的畜栏数
cin>>N;
for(int i=; i<N; ++i)
{
cin>>cows[i].s>>cows[i].e;
cows[i].no=i;
}
sort(cows,cows+N);
// for(int i=0; i<N; i++)
// cout<<cows[i].s<<cows[i].e<<endl;
for(int i=; i<N; ++i)
{
if(q.empty())//没有畜栏的情况
{
++total;
q.push(stall(cows[i].e,total));
pos[cows[i].no]=total;
}
else
{
stall st=q.top();
if(st.over<cows[i].s)//有畜栏且畜栏结束使用时间小于奶牛开始挤奶的时间
{
q.pop();
q.push(stall(cows[i].e,st.No));
pos[cows[i].no]=st.No;
}
else//有畜栏且畜栏结束使用时间大于/等于奶牛开始挤奶的时间
{
++total;//新增一个畜栏
q.push(stall(cows[i].e,total));
pos[cows[i].no]=total;
}
}
}
cout<<total<<endl;
for(int i=; i<N; ++i)
cout<<pos[i]<<endl;
}
【POJ - 3190 】Stall Reservations(贪心+优先队列)的更多相关文章
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- 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 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
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
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- 走入asp.net mvc不归路:[4]说说Action有哪些常见成员
一个控制器中,功能最终会落实到一个个Action中实现,最常见的是增删查改操作.这些Action是一个个的方法,一般返回值是ActionResult,并且是public 方法,可以带参数,可以添加元标 ...
- C#语言 数组
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Celeron
http://en.wikipedia.org/wiki/Celeron Celeron From Wikipedia, the free encyclopedia Celeron Produ ...
- Thread Runnable 区别
[线程的并发与并行] 在单CPU系统中,系统调度在某一时刻只能让一个线程运行,虽然这种调试机制有多种形式(大多数是时间片轮巡为主),但无论如何,要通过不断切换需要运行的线程让其运行的方式就叫并发(co ...
- HBase在滴滴出行的应用场景和最佳实践
摘要: 主要介绍了HBase和Phoenix在滴滴内部的一些典型案例.文章已在CSDN极客头条和<程序员>杂志发表,应朋友邀请,分享到云栖社区,希望给大家带来启发和帮助. 背景 对接业务类 ...
- RSA前端JS加密,后端JAVA解密实现
用RSA非对称加密方式实现.后台生成rsa密钥对,然后在页面设置rsa公钥,提交时用公钥加密密码,生成的密文传到后台,后台再用私钥解密,获取密码明文.这样客户端只需要知道rsa加密方式和公钥,前台不知 ...
- CLI和CGI的区别
CGI :“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.以CGI方式运行时,web s ...
- KbmMW资源汇总(特别是xalion的文章)
KbmMW框架是收费的,不在此提供下载,如需购买,请自行联系作者Kim Madsen. 网址资源: 官网主页:http://www.components4programmers.com/product ...
- java 正则表达式 -Regular Expression
正则表达式(Regular Expression),可以说就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式.正则表达式定义了字符串的模式,可以用来搜索.编辑或处理文本,不仅限于某一种语言(P ...
- uuid.js
// On creation of a UUID object, set it's initial valuefunction UUID(){ this.id = this.createUUID ...