POJ-3190-Stall Reservations
来源:互联网 发布:淘宝上的哑铃凳哪个好 编辑:程序博客网 时间:2024/05/16 19:38
这个题是说一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。
思路:
最开始想的是以奶牛要求时间的结束点从小到大进行排序,但后来发现这样的想法是错误的。后来经过调整,应该先按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先。然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int maxn=60000;int n,use[maxn];struct Node{ int l; int r; int pos; bool operator <(const Node &a)const {if(r==a.r) return l>a.l;return r>a.r; }}a[maxn];priority_queue<Node> q;bool cmp(Node a,Node b){ if(a.l==b.l)return a.r<b.r; return a.l<b.l;}int main(){ while(scanf("%d",&n)!=EOF) {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);q.push(a[0]);int now=0,ans=1;use[a[0].pos]=1;for(int i=1;i<n;i++){ if(!q.empty()&&q.top().r<a[i].l) {use[a[i].pos]=use[q.top().pos];q.pop(); } else {ans++;use[a[i].pos]=ans; } q.push(a[i]);}printf("%d\n",ans);for(int i=0;i<n;i++) printf("%d\n",use[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
- c#"分析 EntityName 时出错"的解决方案
- 小鑫の日常系列故事(七)——小纸条
- C# UTF-8 去BOM头
- 安装svn服务器端及客户端详细步骤
- C++ 注册表编程
- POJ-3190-Stall Reservations
- Shell脚本 ssh远程复制文件与vi编辑器
- Awk 实例,第 2 部分
- poj----3176
- C#字符串加密和解密
- Awk 实例,第 3 部分
- 交叉编译ncurses5.6
- android 中同步用户数据的方式
- eclipse配置jdk