一、题目

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 区间问题的更多相关文章

  1. 【贪心算法】POJ-1328 区间问题

    一.题目 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, ...

  2. 【贪心算法】POJ-2376 区间问题

    一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...

  3. POJ 3190 Stall Reservations贪心

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

  4. 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题

    1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...

  5. 贪心算法----区间选点问题(POJ1201)

    题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...

  6. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  7. poj 1088 滑雪(贪心算法)

    思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...

  8. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

  9. 【POJ - 3190 】Stall Reservations(贪心+优先队列)

    Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...

随机推荐

  1. php基础知识考察点:自定义函数及内部函数考察点

    1.变量的作用域和静态变量 函数的参数以及参数的引用传递 函数的返回值以及引用返回 外部文件的导入 系统内置函数的考察 变量的作用域也称为变量的范围,变量的范围即他定义上下文的背景(也是它生效的范围) ...

  2. python3爬虫-使用requests爬取起点小说

    import requests from lxml import etree from urllib import parse import os, time def get_page_html(ur ...

  3. Truck History(prime)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31871   Accepted: 12427 D ...

  4. 排序算法:快速排序解析及Python实现

    关键词:分而治之.递归.计算速度.基准值 1. 什么是分而治之? 1.1 分而治之(divide and conquer)一种递归式方法 1.2 找出基线条件,这种条件必须尽可能简单 1.3 不断将问 ...

  5. Kubernetes学习之路(七)之Coredns和Dashboard二进制部署

    一.CoreDNS部署 在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问. (1)编辑coredns.yaml文 ...

  6. .net core中automapper的使用

    automapper 是将两个类中的相同字段进行映射,也可以指定字段进行映射:将 UserDao的id 映射为 User 的age CreateMap<UserDao, User>() . ...

  7. bzoj 3232: 圈地游戏

    bzoj 3232: 圈地游戏 01分数规划,就是你要最大化\(\frac{\sum A}{\sum B}\),就二分这个值,\(\frac{\sum A}{\sum B} \geq mid\) \( ...

  8. 【JUC源码解析】DelayQueue

    简介 基于优先级队列,以过期时间作为排序的基准,剩余时间最少的元素排在队首.只有过期的元素才能出队,在此之前,线程等待. 源码解析 属性 private final transient Reentra ...

  9. ARP 地址分类 NAT技术

    第1章 OSI回顾 1.1 TCP/IP协议族组成 应用层 主机到主机层   互联网层   网络接入层 1.2 总结应用层掌握的协议与端口号对应关系 http(80) telnet(23) ftp(2 ...

  10. 180807-Quick-Task 动态脚本支持框架之Groovy脚本加载执行

    Quick-Task 动态脚本支持框架之Groovy脚本加载执行 上一篇简答说了如何判断有任务动态添加.删除或更新,归于一点就是监听文件的变化,判断目录下的Groovy文件是否有新增删除和改变,从而判 ...