Stall Reservations
来源:互联网 发布:java qq2007.jar 编辑:程序博客网 时间:2024/04/29 21:56
Stall Reservations
题目:挤牛奶,在有时间段的控制下。最少需要多少牛棚。
思路:首先按开始排序,越早开始的越排前面。然后用优先队列取排序好的奶牛,优先取出早结束并开始时间比上一个放入的奶牛的结束时间晚的奶牛放入队列,并将原来的取出,如没有合适的奶牛。则使当前牛棚的编号加一,直接放入排序好的第二个奶牛。不必取出原来的奶牛,因为因为原来的奶牛是视为未放入牛棚。
主要代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 55000;struct stall{ int x,y,p; bool operator < (const stall &b)const//默认排序顺序,如果同时结束,则早开始的优先。 { if(y == b.y) { return x > b.x; } else { return y > b.y; } }}cow[maxn];bool cmp(stall a, stall b)//如果同时开始,则早结束的在前面{ if(a.x == b.x) { return a.y < b.y; } else { return a.x < b.x; }}priority_queue<stall> q;int main(){#ifdef LOCAL freopen("dd.txt", "r", stdin);#endif int n; while(~scanf("%d", &n)) { for(int i = 0; i < n; i++) { scanf("%d%d" , &cow[i].x,&cow[i].y); cow[i].p = i; } sort(cow, cow + n,cmp); q.push(cow[0]);//将第一个牛放入 int Stall = 1,use[maxn];//建立牛棚,并用use来存牛的所在牛棚。 use[cow[0].p] = 1; for(int i = 1; i < n; i++) { if(!q.empty()&&q.top().y<cow[i].x) { use[cow[i].p] = use[q.top().p]; q.pop();//如果不需要新牛棚就弹出,并将原来的牛棚的最后一头牛的位置给出 } else { Stall++; use[cow[i].p] = Stall;//需要就换一个牛棚,其实就是把 } q.push(cow[i]); } printf("%d\n", Stall); for(int i = 0 ; i < n; i++) { printf("%d\n", use[i]); } while(!q.empty()) { q.pop();//初始化 } } return 0;}其实蛮水的,据说可以用线段树,这里就不给出代码了。
0 0
- Stall Reservations
- Stall Reservations
- poj 3190 Stall Reservations
- POJ-3190-Stall Reservations
- POJ-3190-Stall Reservations
- POJ 3190 Stall Reservations
- POJ-3190-Stall Reservations
- POJ3190 Stall Reservations 贪心
- poj 3190Stall Reservations
- poj 3190 Stall Reservations
- Stall Reservations(3190)
- POJ3190 Stall Reservations 【贪心】
- poj 3190 Stall Reservations
- Stall Reservations - POJ3190 贪心
- Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- 类集-List接口
- [Jquery]事件的切换和删除
- libevent源码解析------日志和错误处理
- final
- 【Leetcode】Remove Duplicates from Sorted Array
- Stall Reservations
- 【论文笔记】T-Finder-A Recommender System for Finder Passengers and vacant Taxis
- iOS开发之UIButton
- git 命令使用
- ArrayList利用泛型对象操作
- 关于spring 3.2 @scheduled cron的
- c语言基础知识
- c3p0.properties
- Codeforces Round #334 (Div. 2) B. More Cowbell