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

时间: 2024-10-09 19:04:29

poj3190 Stall Reservations(贪心+STL)的相关文章

POJ3190 Stall Reservations 贪心

这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前区间时,提取出所有线程中最后一个被服务中的区间中右端点最小的区间(可用小根堆实现),若当前区间左端点值大于提取出的区间的右端点的值,则把当前区间安排到选中的区间的那个线程,否则只能再派出一个线程来负责该区间了. 此贪心是正确的,因为正在被服务中的区间中右端点最小的区间,能使当前区间不被该线程负责的当

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

POJ 3190 Stall Reservations贪心

POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obvi

Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a

POJ3190 Stall Reservations 【贪婪】

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 1117   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

POJ 3190 Stall Reservations(贪心)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3590   Accepted: 1284   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

Stall Reservations(POJ 3190 贪心+优先队列)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 1588   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

Stall Reservations (poj 3190 贪心)

Language: Default Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3394   Accepted: 1215   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over so

poj3190Stall Reservations(贪心+优先队列)

题目链接: 啊哈哈,点我点我 思路: 首先根据挤奶时间的先后顺序排序...然后将第一头牛加入优先队列..然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高... 然后从前向后枚举.如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房..然后从优先队列中删除这头牛..那么这个问题就得到解决了... 题目: Language: Default Stall Reservations Time Limit: