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. Mysql数据库存储emoji表情

    emoji表情需要使用编码格式未utf8mb4,mysql数据库版本要5.5以上,我用的是5.6,因为只有5.5以上支持utf8mb4. 1.数据库编码设定为utf8mb4,如果建库时指定的是utf8 ...

  2. Spring 3整合Quartz 2实现手动设置定时任务:新增,修改,删除,暂停和恢复(附带源码)

    摘要:在项目的管理功能中,对定时任务的管理有时会很常见.但一般定时任务配置都在xml中完成,包括cronExpression表达式,十分的方便.但是如果我的任务信息是保存在数据库的,想要动态的初始化, ...

  3. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  4. Zabbix 3.2.6安装过程

    以3.2.6版本的Zabbix为例展开说明 1.准备Lnmp环境. 本次准备的环境: Linux:2.6.32-642.el6.x86_64 Nginx:1.12.0 Mariadb:10.2.6 P ...

  5. Testlink安装步骤Checking if C:\inetpub\wwwroot\testlink-1.9.3\gui\templates_c directory is writable Failed !

    Testlink安装过程中问题现象: Checking if C:\inetpub\wwwroot\testlink-1.9.3\gui\templates_c directory is writab ...

  6. iOS 输入限制之 InputKit

    前言 最近接手了两个 O2O 的老项目,其中的 Bug 也不言而喻,单看项目中的布局就有 n 种不同的方式,有用纯代码的,有用 Masonry 的,有用 VFL 的,也有用 Xib 的,更有用代码约束 ...

  7. [leetcode-581-Shortest Unsorted Continuous Subarray]

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  8. 【Android Developers Training】 92. 序言:使用同步适配器传输数据

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

  9. 『转』MarsEdit快速插入源代码

    开始用MarsEdit来写博文,客户端的,毕竟是要方便的多啊. 遇到的第一个问题就是:MarsEdit没有提供快速插入源代码的工具,而对于我这枚码农而言,这个就有点太杯具了. 简单研究了一下,发现Ma ...

  10. Vivo展柜灯怎样设计才吸引大量客户?

    1.vivo展柜灯计划的目标是使消耗者在无限的时空中最无效地承受信息.因而,vivo展柜灯计划便是围绕着怎样无效地进步展现活动的服从和质量停止的.除了展现环境本身的计划之外,展现对象陈列方式的计划也是 ...