【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]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- 一览新的 Android Gradle 构建工具:新的 DSL 结构 和 Gradle 2.5
译者地址:[翻]一览新的 Android Gradle 构建工具:新的 DSL 结构 和 Gradle 2.5 原文:First Look at New Android Gradle Build To ...
- Git的使用 -- 用git玩翻github,结尾有惊喜!有惊喜!有惊喜!林妙妙看了说:牛呲呼啦带闪电 (三)(超详解)
简介 上一篇主要讲解的是Git安装及配置,这一篇就详细的从无到有的来用Git玩翻github. 一.什么是Github Github是全球最大的社交编程及代码托管网站(https://github.c ...
- 面向对象 委托变量和this的使用
委托方法: this的使用:
- C语言语句
/*Console.Write("你能跑得过豹子吗,请输入 能/不能:"); string a = Console.ReadLine();//接收所输入的字符串内容, if (a= ...
- Expression Tree 学习笔记(一)
大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...
- java设计模式----真实世界的模式
设计模式的定义: 模式是在某情境下,针对某问题的某种解决方案 反模式: 告诉你如何采用一个不好的解决方案解决一个问题 要点: 1.让设计模式自然而然地出现在你的设计中,而不是为了使用而使用 2.设计模 ...
- C++类中使用new及delete小例子
//默认复制构造函数的不足//尽管有默认的复制构造函数来解决一般对象与对象之间的初始化问题, 但是在有些情况下我们必须手动显式的去定义复制构造函数, 例如: #include <iostream ...
- MRUnit测试
新建一个专门的测试类,代码如下: wordcount的map函数输入string line, 输出<单词 , 1> 右键-> run as junit 出错了,因为输出不是 ...
- swing _JFileChooser文件选择窗口
import javax.swing.JFileChooser; import org.eclipse.swt.internal.win32.TCHITTESTINFO; public class t ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...