【POJ】3190
来源:互联网 发布:中国中元国际待遇 知乎 编辑:程序博客网 时间:2024/06/15 15:16
http://poj.org/problem?id=3190
n头奶牛要在指定的时间内挤牛奶,一台机器只能对一个奶牛工作。给定每头奶牛的开始时间和结束时间,问最少需要多少机器,并输出每只牛所在的机器序号。
先按奶牛的开始时间从小到大排序,然后维护一个优先队列,以奶牛的结束时间小为优先。每次检查当前奶牛的开始时间是否大于堆顶牛奶的结束时间。若是,进入该机器挤奶。若否,增加一台新机器。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace std;int n;int now,ans;struct Cow{ int st,ed; int num; int now; bool operator< (const Cow &a)const{ return ed>a.ed; } //这里使用 friend operator<(Cow a,Cow b){ ... } 报错}cow[50005];bool cmp1(Cow a,Cow b){ return a.st<b.st;}bool cmp2(Cow a,Cow b){ return a.num<b.num;}priority_queue <Cow> q;int main(){ cin >> n; for (int i=0;i<n;i++){ int x,y; cow[i].num=i; scanf("%d%d",&x,&y); cow[i].st=x; cow[i].ed=y; } sort(cow,cow+n,cmp1); cow[0].now=1; q.push(cow[0]); ans=1; for (int i=1;i<n;i++){ if (cow[i].st>q.top().ed){ cow[i].now=q.top().now; q.pop(); q.push(cow[i]); } else{ ans++; cow[i].now=ans; q.push(cow[i]); } } printf("%d\n",ans); sort(cow,cow+n,cmp2); for (int i=0;i<n;i++){ printf("%d\n",cow[i].now); }}
阅读全文
0 0
- POJ 3190
- poj 3190
- 【POJ】3190
- poj 3190
- poj 3190
- 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 Reservations1
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 解题报告
- poj 3190 Stall Reservations
- js判断
- Leetcode:First Missing Positive
- 234. Palindrome Linked List
- Java注解(Annotation)自定义注解入门
- HTTP详解
- 【POJ】3190
- 文件解析漏洞总结-Nginx
- 年少成名的我并没有放弃自己,谁敢说她\他文章比我写的好?!,不服来战!
- Altium Designer中如何进行覆铜和网状覆铜?
- 简化java开发
- smail的语法
- 8月18日总结
- QT+Opencv实现人脸检测与性别识别(2)
- MapReduce数据倾斜解决方案1--1、重新设计key---二次作业