Stall Reservations
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
Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
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
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的更多相关文章
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 509 Sol ...
- Stall Reservations(POJ 3190 贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4434 Accepted: 158 ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )
线段树.. -------------------------------------------------------------------------------------- #includ ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 553 ...
- POJ3190 Stall Reservations 【贪婪】
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3106 Accepted: 111 ...
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 566 Sol ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
随机推荐
- Centos6.9minimal安装图形化界面
有时我们会用到图形化界面来操作,下面介绍是在虚拟机上安装Centos6.9minimal版安装图形化界面(其他系统版本都类似吧,,,),如果是在物理机上安装进入的话要用的远程桌面工具VNC. VNC安 ...
- 关于ZK框架的onScroll事件的问题
由于我现在所在的公司用到的zk框架,遇到了一个需求frozen on top. 简单来说就是滚动超过范围后,希望有一块东西停留在滚动窗口的顶部. 一.zk框架 查看了zk的8.x版本,发现组件的支持的 ...
- Python scrapy框架
Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...
- angular中使用echart遇到的获取容器高度异常的问题记录
问题 在使用echart去创建图表时,发现图表只占了容器的一个角落,如图,并没有充满容器. 第一反应是容器元素的样式有问题,于是我把容器的宽高都改为px指定的(之前是百分比设定的,查询资料发现说ech ...
- C++ STL基本容器使用
1:关联容器和顺序容器 c++中有两种类型的容器:顺序容器和关联容器,顺序容器主要有:vector.list.deque等.其中vector表示一段连续的内存地址,基于数组的实现,list表示非连续的 ...
- 笔记本电脑连wifi然后通过有线网口做桥接
让你的笔记本电脑作为主机,台式机通过通过一根网线连接到你的笔记本,共享无线网络上网,可以进行如下操作: 1,先找跟网线将两台电脑连接. 2,打开win7自带的windows防火墙,此步在控制面板里可以 ...
- vue2.0 带头冲锋(打包时,小心萝卜坑)
距离上一期,时间间距可能有点长.谁让本人处于兴奋状态,生活已经不能自理. 哈哈哈,嗯,正经一下, 在已有的经验里总结一下那些容易抓狂的坑! 起因:npm run build 打包 本地运行,你以为可以 ...
- javaIO流--Writer,Reader
Writer /** *<li> Writer中定义的一个重要的方法: * public void writer(String str)throws IOException; */ pac ...
- Redis常用命令--Keys
Redis是一个key-value型的数据库. 所以在Redis也提供了很多操作key的命令,大概有22个. EXISTS key [key ...]:查询一个key是否存在,时间复杂度为O(1),存 ...
- [NOI 2014]魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...