【贪心算法】POJ-3190 区间问题
一、题目
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.
二、思路&心得
- 贪心策略:先将所有数据按照开始时间start从小到大进行排序,然后以结束时间end为关键字维护一个最小优先队列。
- 开始时先将排序后的第一个数据加入到优先队列中,然后依次扫描数据,若start大于队首元素的end值,则弹出队首元素,并将此时的数据加入到优先队列中,同时更新每个元素对应的stall[i]值。
- 算法结束时,队列的大小即为所需要的stall个数。
- 在定义结构体时,加入pos位置元素,以保存每个数据对应的原始位置,因为输出要求按照原始数据的顺序进行输出的。
三、代码
#include<cstdio>
#include<queue>
#include<algorithm>
#define MAX_SIZE 50005
using namespace std;
struct P {
int start;
int end;
int pos;
} a[MAX_SIZE];
int N;
int stall[MAX_SIZE];
bool cmp(P a, P b) {
return a.start < b.start;
}
bool operator > (P a, P b) {
return a.end > b.end;
}
void solve() {
priority_queue<P, vector<P>, greater<P> > que;
for (int i = 0; i < N; i ++) {
scanf("%d %d", &a[i].start, &a[i].end);
a[i].pos = i;
}
sort(a, a + N, cmp);
fill(stall, stall + N, 1);
que.push(a[0]);
for (int i = 1; i < N; i ++) {
P temp = que.top();
if (a[i].start > temp.end) {
stall[a[i].pos] = stall[temp.pos];
que.pop();
} else {
stall[a[i].pos] = que.size() + 1;
}
que.push(a[i]);
}
printf("%d\n", que.size());
for (int i = 0; i < N; i ++) {
printf("%d\n", stall[i]);
}
}
int main() {
scanf("%d", &N);
solve();
return 0;
}
【贪心算法】POJ-3190 区间问题的更多相关文章
- 【贪心算法】POJ-1328 区间问题
一.题目 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, ...
- 【贪心算法】POJ-2376 区间问题
一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- 贪心算法----区间选点问题(POJ1201)
题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- poj 1088 滑雪(贪心算法)
思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...
- POJ 2287 田忌赛马 贪心算法
田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- mysql/mariadb学习记录——查询
连接查询:同时设计两个及以上的表的查询 连接条件或连接谓词:用来连接两个表的条件一般格式: [<表名1>]<列名1> <比较运算符> [<表名2>]&l ...
- PHP+Ajax+plupload无刷新上传头像代码
很简单的一款PHP+Ajax+plupload无刷新上传头像代码,兼容性很好,可以直接拿来用.你可以自定义各种类型的文件.本实例中只能上传"jpg", "png" ...
- Wifi NAT Driver
最近遇点奇葩问题,最开始以为被种马了经检测发现不是,分享下以后大家出现类似也好解决:同时列出尝试过程以便大家以后自行排除自己网络问题: 症状: 网络连接右下角图标显示为网卡无线网卡和有线均禁用状态,查 ...
- 2017-2018-1 20155320 课堂测试(ch06)
2017-2018-1 20155320 课堂测试(ch06) 1.(单选题 | 1 分) 下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为(D) A . 1 B . 1/ ...
- python基础学习1-迭代器
#!/usr/bin/env python # -*- coding:utf-8 -*- #自定义迭代器 需要 重写 __iter__()和__next__() 两个魔法方法 class Fibs: ...
- 前端- JavaScript - 总结
1.JavaScript的介绍 javaScript是一种web前端的描述语言,也是一种基于对象(object)和事件驱动(Event Driven)的.安全性好的脚本语言. 它运行在客户端从而减轻服 ...
- OpenStack入门篇(二十)之实现阿里云ESC多FLAT网络
1.给两台虚拟机增加网卡,使用仅主机模式,网段为:192.168.57.0/24 2.修改两台主机网卡配置 [root@linux-node1 ~]# cp /etc/sysconfig/networ ...
- Oracle保存带&的数据
在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符号,SQL*Plus就会要你输入替代值.这就意味着你无法将一个含有该符号的字符串输入数据库或赋给 ...
- Hbase第五章 MapReduce操作HBase
容易遇到的坑: 当用mapReducer操作HBase时,运行jar包的过程中如果遇到 java.lang.NoClassDefFoundError 类似的错误时,一般是由于hadoop环境没有hba ...
- browser-sync 文件监听失败的解决方案
问题 为了方便实时预览前端开发过程中修改源码后的页面,我在全球最大的同性交友网Github中找到了一个非常实用的工具,browser-sync. 安装使用方式请自行到官网https://browser ...