POJ 3190 Stall Reservations贪心

Description

  Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

  Help FJ by determining:

    •   The minimum number of stalls required in the barn so that each cow can have her private milking period
    •   An assignment of cows to these stalls over time

  Many answers are correct for each test dataset; a program will grade your answer.

Input

  Line 1: A single integer, N

  Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

  Line 1: The minimum number of stalls the barn must have.

  Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

 
Sample

Sample Input

Sample Output

题意:

  一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。

思路:

  按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先。然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。这个题是会场安排问题的升级版,题目链接:这里,将会场举行的每一场写出来。这里绝不可以两层循环,两层循环会超时,用优先队列代替一层循环写不会超时。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
int n,use[maxn];
struct Node
{
int left;//开始时间
int right;//结束时间
int pos;//记录标号用来排序
friend bool operator <(Node a,Node b)
{
if(a.right==b.right)
return a.left>b.left;
return a.right>b.right;
}//优先队列按照结束时间从小到大排序,结束时间相等,开始时从小到大排序
} a[maxn]; priority_queue<Node> q; bool cmp(Node a,Node b)
{
if(a.left==b.left)
return a.right<b.right;
return a.left<b.left;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=; i<n; i++)
{
scanf("%d%d",&a[i].left,&a[i].right);
a[i].pos=i;
}
sort(a,a+n,cmp);//按照结束时间从小到大排序
q.push(a[]);
int now=,ans=;
use[a[].pos]=;
for(int i=; i<n; i++)
{
if(!q.empty()&&q.top().right<a[i].left)
{
use[a[i].pos]=use[q.top().pos];
q.pop();
}
else
{
ans++;
use[a[i].pos]=ans;
}
q.push(a[i]);
}
printf("%d\n",ans);
for(int i=; i<n; i++)
printf("%d\n",use[i]);
while(!q.empty())
q.pop();
}
return ;
}

POJ 3190 Stall Reservations贪心的更多相关文章

  1. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  2. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  3. poj 3190 Stall Reservations

    http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  4. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  5. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  6. POJ -3190 Stall Reservations (贪心+优先队列)

    http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...

  7. POJ 3190 Stall Reservations 【贪心 优先队列】

    题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...

  8. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

  9. [POJ3197]Stall Reservations (贪心)

    题意 (来自洛谷) 约翰的N(l<N< 50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当 然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段 ...

随机推荐

  1. TIOBE:全球编程语言最新排名(Kotlin排名进入前50名)

    作为coder,大家当然关心自己所使用语言的应用趋势.要是几年后所用语言变得默默无闻,那岂不是之前的知识储备与经验积累都会大打折扣.TIOBE排行榜是根据互联网上有经验的程序员.课程和第三方厂商的数量 ...

  2. ecshop 商品分类页 取得当前分类下的子分类方法

    ecshop的商品分类页面category.php 下的分类,默认是取得所有同级父分类以及父类别的子分类.比如,我点击进入是A商品分类的页面 category.php?id=1,事实上 我只需要取得父 ...

  3. Python操作redis系列之 列表(list) (四)

    # -*- coding: utf- -*- import redis r =redis.Redis(host=,password="ZBHRwlb1608") 1. Lpush ...

  4. php 函数形参前面加上&

    <?php function test(&$a){ $a=$a+100; } $b=1; echo $b;//输出1 test($b);//这里$b传递给函数的其实是$b的变量内容所处的 ...

  5. 【Android Developers Training】 94. 创建一个空内容提供器(Content Provider)

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. example001点击隐藏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. React 在服务端渲染的实现

    原文地址:Server-Side React Rendering 原文作者:Roger Jin 译者:牧云云 React 在服务端渲染的实现 React是最受欢迎的客户端 JavaScript 框架, ...

  8. Java程序设计---io流读取文件内容并将其逆值输出到控制台

    import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileR ...

  9. js代码风格之链式结构

    <div class="box"> <ul class="menu"> <li class="level1"& ...

  10. JQuery实战——页面进度条效果

    今早逛阮一峰大神的博客 ECMAScript 6 入门 时候看到页面顶部有个进度条显示当前浏览的进度,如图: 顶部进度条会根据当前页面高度进行宽度调整,实战一番,视觉使用animated方法实现.下面 ...