Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7646   Accepted: 2710   Special Judge

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 Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample:

Here's a graphical schedule for this output:

Time     1  2  3  4  5  6  7  8  9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..

Stall 3 .. .. c3>>>>>>>>> .. .. .. ..

Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

Other outputs using the same number of stalls are possible.

 
 
这道大水题真的坑,不少人想的都是贪心,背包问题。比赛时候甚至队友讨论过树状数组。。。。。。。
比赛结束鹏哥说C题E题都是水题,E题优先队列。   喵喵喵???
仔细一想真的是啊。根本没往这方面想过!!
每次看完题解都是这么简单怎么没想出来,允悲。。。
 
 
 
这道题是修改了队列的优先级,因为在同一槽中要下一只牛的话, 当前r值必须小于l值。所以优先级r值优先。
所以本题的难点就在于,怎样处理当前槽,怎样构造队列优先级!!
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int l,r,n;
bool operator <(const node &b)const
{
return r>b.r||(r==b.r&&l>b.l);
}
}a[50005];
priority_queue <node> q;
int u[50005];
bool cmp(node a,node b)
{ return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
int ans=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].n=i;
}
sort(a+1,a+n+1,cmp);
q.push(a[1]);
u[a[1].n]=1; for(int i=2;i<=n;i++)
{
if(!q.empty()&&q.top().r<a[i].l){
u[a[i].n]=u[q.top().n];
q.pop();
}
else{
ans++;
u[a[i].n]=ans;
}
q.push(a[i]);
}
printf("%d\n",ans);
for(int i =1;i<=n;i++){
printf("%d\n",u[i]);
}
while(!q.empty()) // 尤其重要,排空队列
q.pop();
}
}

  

POJ 3190 Stall Reservations (优先队列)C++的更多相关文章

  1. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  2. poj 3190 Stall Reservations

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

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

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

  4. POJ 3190 Stall Reservations【贪心】

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

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

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

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

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

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

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

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

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11002   Accepted: 38 ...

  9. POJ:3190-Stall Reservations

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

随机推荐

  1. vue.js + ajax 数据加载(纯新手get)

    似懂非懂的感觉下撸了一个小demo .(只是单纯的引用vue.js的小demo.)在实践中进步吧! 首先肯定要先看vue的文档,并且知道超超基础的小知识!!!奉上代码: HTML 部分: js的引用: ...

  2. 第4天:JS入门-给div设置宽高背景色

    今天学习了js入门课程,听的不多,做了个小练习,给div设置宽高.背景色.一点点都是进步.核心代码如下: <!DOCTYPE html><html lang="en&quo ...

  3. iOS app 逆向过程(持续更新完善)

    一.获取待逆向的app 1.用cyder2直接从源里下载,适合逆向越狱开发的软件. 2.从pp助手中下载,pp助手中有越狱应用和正版应用.越狱应用直接是已经脱壳的,未越狱应用还需要执行砸壳 二.获取待 ...

  4. c#Reverse字符串

    class Program { static void Main(string[] args) { // 输出 ypoc si yek eht string str = "the key i ...

  5. 【Java学习笔记之二十八】深入了解Java8新特性

    前言: Java8 已经发布很久了,很多报道表明java8 是一次重大的版本升级.在Java Code Geeks上已经有很多介绍Java 8新特性的文章,例如Playing with Java 8 ...

  6. 新瓶装旧酒:全程无命令 GitHub Pages 创建您的博客站点

    使用 GitHub Pages 创建博客站点的文章很多,也有很长的历史了.但是,许多已经与当前的 GitHub 不一致了,如果你按图索骥,会发现驴唇对不上马嘴. 更为麻烦的是,你会发现或者需要你输入许 ...

  7. iptables规则的删除-怎么删除一条已有的iptables规则

    语法是: iptables -D chain rulenum [options]     其中: chain 是链的意思,就是INPUT FORWARD 之类的定语     rulenum 是该条规则 ...

  8. 记一次Spring aop的所遇到的问题

    由来 项目中需要实现某个订单的状态改变后然后推送给第三方的功能,由于更改状态的项目和推送的项目不是同一个项目,所以为了不改变原项目的代码,我们考虑用spring的aop来实现. 项目用的是spring ...

  9. 电源库(Sources)

  10. Java温故而知新-空心菱形

    空心菱形 今天做题练手,题目是空心菱形,一开始没什么思路,去网上找了找,发现很难找到讲的通透的,自己现在独立做出来了,记录一下,以备后用. * * * * * * * * * * * * * * * ...