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.

 
题解:整理区间,将不重合的区间整理成一个区间
需要用到优先队列
当优先队列的元素是结构体时候,需要在结构体内重载比较操作符函数。
 struct node{
int a;
int b;
int flag;
friend bool operator <(node node1,node node2)
{
//<为从大到小排列,>为从小到大排列
return node1.b<node2.b;
}
}n[];
 #include<iostream>
#include<algorithm>
#include<queue>
using namespace std; struct node{
int a; //开始时间
int b; //结束时间
int c; //序列号
int flag; //区间安排序号
friend bool operator <(node node1,node node2) //重载比较操作符函数
{
return node1.b > node2.b;
}
}cows[]; bool cmp1(node t1,node t2) //根据区间排序
{
if(t1.a != t2.a) return t1.a < t2.a;
else return t1.b < t2.b;
} bool cmp2(node t1,node t2) //根据序列号排序
{
return t1.c < t2.c;
} int main()
{
int n;
priority_queue<node>que;
while(~scanf("%d",&n))
{
for(int i = ; i < n; i++)
{
scanf("%d %d",&cows[i].a,&cows[i].b);
cows[i].c = i;
}
sort(cows,cows+n,cmp1);
int ans = ;
cows[].flag = ans;
que.push(cows[]); for(int i = ; i < n; i++)
{
if(que.top().b >= cows[i].a) //此时cows的区间与que内的任意区间有重合
{
ans++;
cows[i].flag = ans;
}
else
{
cows[i].flag = que.top().flag;
que.pop();
}
que.push(cows[i]);
} sort(cows,cows+n,cmp2);
cout<<ans<<endl;
for(int i = ; i < n; i++)
{
printf("%d\n",cows[i].flag);
}
}
return ;
}

Stall Reservations的更多相关文章

  1. poj 3190 Stall Reservations

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

  2. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

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

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 158 ...

  4. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

    线段树.. -------------------------------------------------------------------------------------- #includ ...

  5. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...

  6. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  7. 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 566  Sol ...

  8. POJ 3190 Stall Reservations贪心

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

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

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

  10. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

随机推荐

  1. MVC、MVP以及MVVM分析

    网上现在MVC.MVP以及MVVM的讲解一搜一箩筐,根据了网上大多数的文章,根据我的思考习惯进行了总结. MVC介绍及分析: 各层的职责如下所示: Models: 数据层,负责数据的处理和获取的数据接 ...

  2. python3.6执行pip3时 Unable to create process using '"'

    问题需求 由于在windows操作系统中已经安装了python2.7,要在安装python3的时候 将python3.6安装在C:\Python36目录下 然后进入C:\Python36目录下执行pi ...

  3. codeforces round 425 div2

    A. Sasha and Sticks 水题,判断一下次数的奇和偶就可以的. B. Petya and Exam 赛上的时候没有写出来,orz,记录一下吧. 题意:给出一个模式串,可能会有?和*两种符 ...

  4. SpringMVC 使用MultipartFile实现文件上传(转)

    http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...

  5. JVMGC机制

    GC 是JVM的垃圾回收器.与C/C++不同,java程序员无需考虑太多内存分配的位置,更不用考虑内存释放的机制,java对象内存的申请和释放都有JVM托管.JVM的内存释放机制就是GC. GC的过程 ...

  6. java8接口定义增强

    java1.7之前,接口中只允许有全局常量和抽象方法,而1.8之后允许在接口中扩充default修饰的普通方法和static修饰的静态方法 其目的是在修改接口中方法的时候,子类就不必去一一修改 pac ...

  7. [C#]设计模式-简单工厂-创建型模式

    在设计模式当中有三大工厂,分别是 简单工厂.抽象工厂.工厂方法 这三种创建实例的设计模式,这里先从简单工厂将其,从名字就可以看出这是这三种工厂模式当中最为简单的一种实现. 简单工厂一般由以下几个对象组 ...

  8. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  9. angularjs中使用 <input type="file">标签实现一次最多上传5张图片

    前期准备: 1.angular.js 2.bootstrap.css 具体如何实现呢?请看下面代码哈哈哈. 在angular项目中,如果要给<input type="file" ...

  10. spring copy中的一个很气人的问题(初学者渣渣的一些感受)

    把别人的工程直接导入使用,出现了各种bug......(细节决定成败,得到以下教训) 1.工程的第一步是检查版本和插件版本兼容问题.很重要 2.然后导入包,看依赖包是否版本太低,(前期做好这些,能让你 ...