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. H5的canvas绘图技术

    canvas元素是HTML5中新添加的一个元素,该元素是HTML5中的一个亮点.Canvas元素就像一块画布,通过该元素自带的API结合JavaScript代码可以绘制各种图形和图像以及动画效果. 1 ...

  2. 高级控件之Scrollview ( 滑动屏幕 ) 与 Imageview (滑动屏幕 切换图片)

    ScrollView  的xml布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...

  3. Why deep learning?

    1. 深度学习中网络越深越好么? 理论上说是这样的,因为网络越深,参数也越多,拟合能力也越强(但实际情况是,网络很深的时候,不容易训练,使得表现能力可能并不好). 2. 那么,不同什么深度的网络,在参 ...

  4. linux添加超级用户

    创建super账号 useradd testuser 创建用户testuser passwd testuser 给已创建的用户testuser设置密码 如果需要让此用户有root权限,执行命令: ro ...

  5. html标记语言 --框架

    html标记语言 --框架 六.框架 1.什么是框架 框架将浏览器划分成不同的部分,每一部分加载不同的网页 实现同一浏览器窗口中加载多个页面的效果. 语法格式<frameset>..... ...

  6. vue-cli 的项目 切换到Linux环境下遇到问题

    之前用vue-cli脚手架在windows上开发的一个项目,现在有换mac的打算,但是换系统的话对代码对环境依赖比较严重. 去年和别的FE并行开发两个人用的都是windows,这样还好,没有什么问题, ...

  7. [LeetCode] Maximum Binary Tree 最大二叉树

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. [Vijos 1143]三取方格数

    Description 设有N*N的方格图,我们将其中的某些方格填入正整数, 而其他的方格中放入0. 某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角. 在走过的路上,他取走了方格中的数 ...

  9. hihoCoder 1595 : Numbers

    Description You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to ass ...

  10. Java多线程基础总结

    一.线程和进程关系 二.创建方式1.继承Thread类,重写run方法2.实现Runable接口,重写run方法3.使用匿名内部类 三.API接口start()currentThread() 获取当前 ...