POJ-3190-Stall Reservations
来源:互联网 发布:java方向都有哪些 编辑:程序博客网 时间:2024/05/16 13:45
本题地址:http://poj.org/problem?id=3190
本题大意如下:
在一个农场有一群奶牛(它们可以被栅栏分开),它们分别会在某个时间段产奶,但要求是产奶时不会被其它的奶牛打扰!现农场栅栏有限,求最少需要多少个栅栏能让它们将牛奶顺利产完!
把这道题数学化:给你n条线段,问最少需要分成几组,使得每组中的线段互不相交!!!
这个题使用优先队列来解决是很方便的!
代码如下:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#include<iostream>using namespace std;struct COW{ int z,y,p; bool operator<(const COW &cows)const { return y>cows.y;//右区间值小的先出对列! }} cow[52014];priority_queue<COW>Q;int cmp(COW a,COW b){ if(a.z==b.z) return a.y<b.y; return a.z<b.z;}int main(){ int n,num=1;//初始化当前栅栏的个数!; cin>>n; int pos[52014];//这个数组是用来存栅栏编号的! for(int i=0; i<n; i++) { cin>>cow[i].z>>cow[i].y; cow[i].p=i;//先记住位置,因为后面会进行排序,到时原来的顺序会改变! } sort(cow,cow+n,cmp); pos[cow[0].p]=num; Q.push(cow[0]); for(int i=1; i<n; i++) { if((!Q.empty())&&cow[i].z>Q.top().y) { pos[cow[i].p]=pos[Q.top().p]; Q.pop();//表示两个区间此时有无交集,上一区间的右区间比下一区间的左区间值小,可以共用一个栅栏! //否则需要再开辟一个栅栏! } else pos[cow[i].p]=++num; Q.push(cow[i]); } cout<<num<<endl; for(int i=0; i<n; i++) cout<<pos[i]<<endl; 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
- 嵌入式专题: S5PV210 - H264硬件解码(MFC)
- linux中ctime,mtime,atime的区别
- angularjs 数据绑定显示html
- android api分析33 File
- c++中构造函数调用另一构造函数的问题
- POJ-3190-Stall Reservations
- 数学之路-python计算实战(7)-机器视觉-图像产生加性零均值高斯噪声
- solr dataimport clob字段同步遇到的问题
- The SwiftProgramming Language读书笔记第26页
- JavaScript学习笔记 第六记
- 性能测试工作的完整过程,目的,最关键的是什么
- eclipse调试技巧
- 官方nginx Windows版使用说明
- 按比例划线的工具类