POJ 3190 Stall Reservations
来源:互联网 发布:域名记录类型 编辑:程序博客网 时间:2024/05/17 03:55
题意:一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。
题解:感觉是道经典题,遇到很多遍都好像是弃了。。。。 只能借鉴别人思路了。。。。
维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先,然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。
代码如下
#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<algorithm>#include <iostream>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#define INF 0x3f3f3f3f#define maxn 50010using namespace std;typedef long long ll;struct Node{ int l,r,pos; bool operator <(const Node &a)const { if(r==a.r) return l>a.l; return r>a.r; }}a[maxn];int cmp(Node x,Node y){ if(x.l == y.l) return x.r < y.r; return x.l < y.l;}priority_queue<Node> q;int main(){ int n; scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%d%d",&a[i].l,&a[i].r); a[i].pos = i; } sort(a,a+n,cmp); int cnt = 1,ans[maxn] = {0}; ans[a[0].pos] = 1; q.push(a[0]); for(int i = 1; i < n; i++) { if(!q.empty() && q.top().r < a[i].l) { ans[a[i].pos] = ans[q.top().pos]; q.pop(); } else ans[a[i].pos] = ++cnt; q.push(a[i]); } printf("%d\n",cnt); for(int i = 0; i < n; i++) printf("%d\n",ans[i]); while(!q.empty()) q.pop(); return 0;}
0 0
- poj 3190 Stall Reservations
- POJ-3190-Stall Reservations
- POJ-3190-Stall Reservations
- POJ 3190 Stall Reservations
- POJ-3190-Stall Reservations
- poj 3190Stall Reservations
- poj 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- NAND Flash 和 eMMC 的简要区别
- SylixOS Makefile分析
- Boosting算法简介
- 黑马程序员—多态,内部类
- Android的签名机制
- POJ 3190 Stall Reservations
- UITextField
- 为SQL增加自定义函数约束
- FreeBSD
- VI文本编辑器
- IO Visor:又一个开源项目横空出世
- 文章怎么还审核呢
- spoj 10232 Distinct Primes(打表)
- HTML <a>标签 title属性 鼠标悬停文字显示