POJ3190
来源:互联网 发布:酷派手机怎么设置网络 编辑:程序博客网 时间:2024/06/06 23:58
Problem: Stall Reservations
Description: 奶牛挤奶用机器的问题,一台机器同一时间只能为一头奶牛挤奶,问你最少需要多少台机器,然后再输出每头奶牛用了哪一台机器。
Solution: 贪心+排序+优先队列,我们开一个优先队列,意思就是队列中的奶牛正在使用机器挤奶。我们比较队头元素和我们排好序的奶牛开始时间,如果排好序的奶牛开始时间早于队头元素结束时间,那么我们就要用一台新机器来挤奶,否则,就可以用队头的机器来服务当前奶牛。
Code(C++):
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>using namespace std;const int M=50005;typedef struct tagTime{ int src,des; int menchine; int pos; tagTime(){} tagTime(int _src,int _des,int _menchine=0): src(_src),des(_des),menchine(_menchine){} bool operator<(const tagTime &other)const{ if(des!=other.des) return des>other.des; return src>other.src; }}Time;int n;Time times[M];int ans[M];int top;int cmp(const void *a,const void *b){ Time *A=(Time *)a; Time *B=(Time *)b; if(A->src!=B->src) return A->src-B->src; if(A->des!=B->des) return A->des-B->des; return A->pos-B->pos;}int main(){ while(~scanf("%d",&n)){ // init for(int i=0;i<n;i++) scanf("%d%d",×[i].src,×[i].des), times[i].pos=i; qsort(times,n,sizeof(times[0]),cmp); priority_queue<Time> que; int cnt=1; for(int i=0;i<n;i++) if(que.empty()||que.top().des>=times[i].src) que.push(Time(times[i].src,times[i].des,cnt)), ans[times[i].pos]=cnt++; else{ int tmp=que.top().menchine; que.pop(); que.push(Time(times[i].src,times[i].des,tmp)), ans[times[i].pos]=tmp; } printf("%d\n",cnt-1); for(int i=0;i<n;i++) printf("%d\n",ans[i]); } return 0;}
1 0
- poj3190
- poj3190
- POJ3190
- poj3190
- POJ3190
- poj3190
- poj3190 贪心
- POJ3190 Stall Reservations 贪心
- poj3190 (mulitset贪心)
- POJ3190 Stall Reservations 【贪心】
- poj3190 区间贪心 + STL
- Stall Reservations - POJ3190 贪心
- POJ3190-Stall Reservations
- poj3190 poj2393 贪心
- poj3190(区间贪心)
- POJ3190 Stall Reservations
- 贪心(好题)--poj3190
- poj3190(贪心,优先队列)
- Java核心知识点-类加载器
- Android 6.0 运行时权限处理完全解析
- Centos 升级glibc
- Hbase数据库简单理解
- JAVAone 总结
- POJ3190
- 输入一个链表,反转链表后,输出链表的所有元素。
- 哈理工OJ 1569 比赛排名(排序和字符串处理)
- qt中获取文件路径和文件名、后缀名
- DICOM格式的图像在MATLAB中处理
- java编程思想第四版中net.mindview.util的jar包导入
- 文本中字符个数统计
- hrbust 1853 哈理工oj 1853 幼稚园的数学Ⅰ【Biginteger】
- Android视频推流直播学习【三】