POJ3190:Stall Reservations(贪心)
来源:互联网 发布:js字符split 编辑:程序博客网 时间:2024/05/22 12:06
Time Limit: 1000MS
Memory Limit: 65536K
Special Judge
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
51 102 43 65 84 7
Sample Output
412324
Hint
Explanation of the sample:
Here’s a graphical schedule for this output:
Other outputs using the same number of stalls are possible.
Source
USACO 2006 February Silver
解题分析
所有奶牛都必须挤奶。到了一个奶牛的挤奶开始时间,就必须为这个奶牛找畜栏。因此按照奶牛的开始时间逐个处理它们,是必然的。
1)把所有奶牛按开始时间从小到大排序。
2)为第一头奶牛分配一个畜栏。
3)依次处理后面每头奶牛
- 若
E(x)<S(i) ,则不用分配新畜栏,i 可进入x ,并修改E(x) 为E(i) 。 - 若
E(x)>=S(i) ,则分配新畜栏y ,记E(y)=E(i) 。
直到所有奶牛处理结束。
需要用优先队列存放已经分配的畜栏,并使得结束时间最早的畜栏始终位于队列头部。
证明:
由于按开始时间的顺序处理奶牛是必然,且按该算法,为奶牛
复杂度:
AC代码
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>using namespace std;struct Cow{ int a,b; int No; bool operator < (const Cow &c) const{return a < c.a;}}cows[50100];int pos[50100];struct Stall{ int et; int No; bool operator < (const Stall &s) const{return et > s.et;} Stall(int e,int n):et(e),No(n){}};priority_queue<Stall>pq;int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%d%d",&cows[i].a,&cows[i].b); cows[i].No=i; } sort(cows,cows+N); int total=0; for(int i=0;i<N;i++){ if(pq.empty()){ total++; pq.push(Stall(cows[i].b,total)); pos[cows[i].No]=total; } else{ Stall st=pq.top(); if(st.et<cows[i].a){ pq.pop(); pq.push(Stall(cows[i].b,st.No)); pos[cows[i].No]=st.No; } else{ total++; pq.push(Stall(cows[i].b,total)); pos[cows[i].No]=total; } } } printf("%d\n",total); for(int i=0;i<N;i++) printf("%d\n",pos[i]); return 0;}
- POJ3190:Stall Reservations(贪心)
- POJ3190 Stall Reservations 贪心
- POJ3190 Stall Reservations 【贪心】
- Stall Reservations - POJ3190 贪心
- #POJ3190#Stall Reservations(贪心 -> 冲突分配)
- POJ3190——Stall Reservations(贪心)
- POJ3190 Stall Reservations (贪心+优先队列)
- POJ3190-Stall Reservations
- POJ3190 Stall Reservations
- POJ 3190 - Stall Reservations(贪心)
- POJ 3190 Stall Reservations(贪心)
- poj 3190 Stall Reservations(贪心)
- [POJ 3190] Stall Reservations (区间贪心)
- Stall Reservations(贪心,****思路理顺)
- Stall Reservations(POJ 3190, 贪心)
- poj Stall Reservations(贪心)
- C - Stall Reservations 贪心算法
- Stall Reservations - POJ 3190 贪心
- 多线程
- 用virtualenv管理python3运行环境 1. 简介 virtualenv可以用来管理互不干扰的独立python虚拟环境,在有些场景下非常有用,例如: 你有两个python项目,
- 批量梯度下降BGD、随机梯度下降SGD、小批量随机梯度下降MSGD
- 自己编写的响应式图片轮播效果
- 【SQL高级教程】数据库学习笔记
- POJ3190:Stall Reservations(贪心)
- PAT乙级1039. 到底买不买(20)
- Hive学习笔记 2 Hive的数据类型
- linux执行.sh文件提示jdk环境问题
- 上传绕过
- 设置让php能够以root权限来执行exec() 或者 shell_exec()
- Longest Run on a Snowboard UVA
- android 修改系统语言默认中文
- Mybatis之mapper XML 文件