poj3190 Stall Reservations(贪心+STL)
https://vjudge.net/problem/POJ-3190
cin和scanf差这么多么。。tle和300ms
思路:先对结构体x升序y升序,再对优先队列重载<,按y升序。
然后依次入队,如果node[i].x<=q.top().y ans++, 否则出队,入队,使用出队的那个摊位。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<set> 8 #define INF 0x3f3f3f3f 9 typedef long long ll; 10 using namespace std; 11 struct Node{ 12 int x, y; 13 int id; 14 bool operator<(const Node &a)const{ 15 return y > a.y; 16 } 17 }node[50010]; 18 int n, maxm = -INF, b[50010]; 19 bool cmp(const Node a, const Node b) 20 { 21 if(a.x != b.x) 22 return a.x<b.x; 23 return a.y<b.y; 24 } 25 int main() 26 { 27 scanf("%d", &n); 28 int ans = 1; 29 for(int i = 0; i < n; i++){ 30 scanf("%d%d", &node[i].x, &node[i].y); 31 node[i].id=i+1; 32 } 33 sort(node, node+n, cmp); 34 priority_queue<Node> q; 35 q.push(node[0]); 36 b[node[0].id] = ans; 37 for(int i = 1; i < n; i++){ 38 Node t = q.top(); 39 if(node[i].x <= t.y){ 40 q.push(node[i]); 41 ans++; 42 b[node[i].id] = ans; 43 //maxm = max(maxm, ans); 44 } 45 else if(node[i].x > t.y){ 46 q.pop(); 47 q.push(node[i]); 48 b[node[i].id] = b[t.id]; 49 //cout << b[t.id] << "t"; 50 } 51 } 52 printf("%d\n", ans); 53 for(int i = 1; i <= n; i++){ 54 printf("%d\n", b[i]); 55 } 56 57 return 0; 58 }
转载于:https://www.cnblogs.com/Surprisezang/p/9000363.html
poj3190 Stall Reservations(贪心+STL)相关推荐
- POJ - 3190 Stall Reservations(贪心+优先队列优化)
题目链接:点击查看 题目大意:有n头牛在畜栏中吃草,每个畜栏在同一时间段只能提供给一头牛吃草,所以可能会需要多个畜栏,给定N头牛和每头牛开始吃草和结束吃草的时间,每头牛在给定时间段内会一直吃草,求需要 ...
- POJ 3190 Stall Reservations(贪心算法)
有 n头牛(1<=n<=50,000)要挤奶.给定每头牛挤奶的时间区 间[A,B] (1<=A<=B<=1,000,000,A,B为整数). 牛需要呆畜栏里才能挤奶.一个 ...
- 【贪心】Stall Reservations(luogu 2859/poj 3190)
Stall Reservations luogu 2859 poj 3190 题目大意: 有n头牛,每头牛都有自己的挤奶时间,挤奶时间内每头牛用一个奶棚,现在问最少需要多少个奶棚 输入样例 5 1 1 ...
- 51nod 1428 bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 951 So ...
- Stall Reservations
题目:Stall Reservations 总结:这道题按照时间的开始的从小到大排序,如果开始时间相同,那么就按照结束时间从小到大,用n2的时间复杂度,只要有后面的开始时间大于前面的结束时间,那么就是 ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 876 So ...
- POJ3190,P2859-Stall Reservations(摊位预订)【贪心,堆】
正题 POJ题目链接:http://poj.org/problem?id=3190 luogu评测记录:https://www.luogu.org/recordnew/lists?uid=52918& ...
- bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】
这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...
- 【POJ3190】Stall Reservations
problem n头奶牛要在指定的时间内吃草,而一个机器只能同时给一个奶牛吃草.给你每头奶牛吃草的开始时间和结束时间,问你最小需要多少机器和每头牛对应的方案. n<=5e4; solution ...
最新文章
- 无向图--邻接矩阵、连接矩阵、深度遍历、广度遍历、生成树
- [YTU]_1046 ( 输入10个数字,然后逆序输出)
- python经典排序_python实现十大经典排序算法
- Windows11 Docker镜像存储路径更改(非C盘路径)
- 常用c语言函数实现,常用的C语言库函数实现
- python中文分词的主要方法
- 倒计时 分秒 小程序 方法_微信小程序 倒计时
- 三菱PLC功能指令详解
- Til the Cows Come Home POJ - 2387
- android中menu重写哪些方法,在Android中Menu的使用
- 若依集成actuator实现优雅关闭应用
- ❀❀❀Excel加载项开发及发布❀❀❀
- 关闭 C4996 警告(_CRT_SECURE_NO_DEPRECATE)方法
- PS工作记录——图片元素复制,智能抠图
- 来吧!带你玩转 Excel VBA
- Golang 加密方法
- 绘制遗传图谱工具——MapChart
- php实现猫眼电影院选座思路,高仿猫眼电影选座(选票)模块
- 基于DSP/BIoS设备驱动模型的视频驱动程序开发
- 安卓访客模式_Android 5.0利用访客模式设置双桌面教程