poj 3190 贪心+优先队列优化
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 4274 | Accepted: 1530 | Special Judge | ||
Description
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
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.
<span style="font-size:18px;color:#3366ff;">#include <iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node{
int s,e,id,num;
bool operator<(const Node &a) const
{
return a.e<e;
}
}node[50005];
int cmp(Node a,Node b)
{
if(a.s!=a.s)
return a.e<b.e;
else return a.s<b.s;
}
int cmp2(Node a,Node b)
{
return a.id<b.id;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d %d",&node[i].s,&node[i].e);
node[i].id=i;
}
sort(node+1,node+n+1,cmp);
priority_queue<Node> q;
node[1].num=1;
q.push(node[1]);
int cnt=1;
for(int i=2;i<=n;i++)
{
Node temp=q.top();
if(node[i].s<=temp.e)
{
cnt++;
node[i].num=cnt;
q.push(node[i]);
}
else
{
node[i].num=temp.num;
q.pop();
q.push(node[i]);
}
}
sort(node+1,node+n+1,cmp2);
printf("%d\n",cnt);
for(int i=1;i<=n;i++)
printf("%d\n",node[i].num);
}
return 0;
}</span>
poj 3190 贪心+优先队列优化的更多相关文章
- Stall Reservations(POJ 3190 贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4434 Accepted: 158 ...
- POJ 2431 贪心+优先队列
题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点. 思路:运用优先队列,将能走到的加油站的油量加入优先队列中, ...
- 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【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- poj 1511 优先队列优化dijkstra *
题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...
- Dijkstra算法(朴素实现、优先队列优化)
Dijkstra算法只能求取边的权重为非负的图的最短路径,而Bellman-Ford算法可以求取边的权重为负的图的最短路径(但Bellman-Ford算法在图中存在负环的情况下,最短路径是不存在的(负 ...
- poj 1862 Stripies/优先队列
原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...
- 【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列+双向链表)
1150: [CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设 ...
随机推荐
- vimdiff、rev命令
一.vimdiff:可视化比较工具 语法: vimdiff [选项] file1 file2 [file3 [file4]] gvimdiff 描述 Vimdiff在两个(或三 ...
- 本地连接Linux工具
连接Linux命令 finaXshell 工具好用: 链接:https://pan.baidu.com/s/13yyOhi7GzcZNTxXseGO_fA 提取码:n4t6 上次Linux 文件工具: ...
- X86逆向8:向程序中插入新区段
本节课我们不去破解程序,本节课学习给应用程序插入一些代码片段,这里我就插入一个弹窗喽,当然你也可以插入一段恶意代码,让使用的人中招, 这里有很多原理性的东西我就不多罗嗦了毕竟是新手入门教程,如果想去了 ...
- element-ui table float类型数据排序失败
背景:对于16.88这样的数据,点击表头排序无效,仍然是乱序 解决办法:自定义排序方法,:sortable="true" :sort-mothod="xxxx" ...
- 树莓派3B+和3B 安装64位debian GUN/Linux系统
请直接参考如下博客: https://blog.csdn.net/u013451404/article/details/80710136 如果是3B的树莓派用户,只需要把第一个分区boot里的.dtb ...
- linq多个条件
public static class PredicateBuilder { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效:单个OR无效,多个 ...
- Mockito中的@Mock和@Spy如何使用
相同点 spy和mock生成的对象不受spring管理 不同点 1.默认行为不同 对于未指定mock的方法,spy默认会调用真实的方法,有返回值的返回真实的返回值,而mock默认不执行,有返回值的,默 ...
- opencv 一些函数的耗时计算
Release 模式 -------------------------------------------------- smooth gaussian : 2 cvtColor CV_BGR2La ...
- cookie转换成字典类型方便scraoy 使用
#bakooie装换成紫电模式方便scrapy使用 cookid = "_ga=GA1.2.1937936278.1538889470; __gads=ID=1ba11c2610acf504 ...
- 记一次配置阿里云ECS GPU计算型gn5实例
基础配置 CPU: Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz * 16 MEM: 120 GiB GPU: NVIDIA P100 * 2 OS: Ubunt ...