poj3190 Stall Reservations
我一开始想用线段树,但是发现还要记录每头cow所在的棚......
无奈之下选择正解:贪心。
用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案。
注意代码实现的细节。
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = ;
struct Cow {
int a, b, num;
bool operator < (const Cow& x) const {
return this->a < x.a;
}
bool operator > (const Cow& x) const {
return this->a > x.a;
}
}c[N];
priority_queue< Cow, vector<Cow>, greater<Cow> >Q;
int ans[N];
int main() {
int n, top = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d%d", &c[i].a, &c[i].b);
c[i].num = i;
}
sort(c + , c + n + );
Cow x;
x.a = c[].b;
x.b = ++top;
ans[c[].num] = top;
Q.push(x);
for(int i = ; i <= n; i++) {
//printf("i:%d c[i].b:%d Q.top().a:%d\n", c[i].num, c[i].b, Q.top().a);
if(Q.top().a < c[i].a) {
x = Q.top();
Q.pop();
x.a = c[i].b;
ans[c[i].num] = x.b;
Q.push(x);
}
else {
x.a = c[i].b;
x.b = ++top;
ans[c[i].num] = top;
Q.push(x);
}
}
printf("%d\n", top);
for(int i = ; i <= n; i++) {
printf("%d\n", ans[i]);
}
return ;
}
AC代码
poj3190 Stall Reservations的更多相关文章
- POJ3190 Stall Reservations 【贪婪】
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3106 Accepted: 111 ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
- poj3190 Stall Reservations(贪心+STL)
https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- POJ3190 Stall Reservations 贪心
这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj3190 stall revertation
Stall Re ...
- BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 509 Sol ...
随机推荐
- cmake : undefined reference to dlopen, dlclose, dlsym and dlerror
链接出了问题 添加头文件 #include <dlfcn.h> 添加库 target_link_libraries(PROJECT_NAME ${CMAKE_DL_LIBS})
- (自用 )npm --save和--save-dev区别
https://blog.csdn.net/playboyanta123/article/details/78349034(原文) 目前大多数基于vue的项目都是用vue-cli 创建的.当创建项目完 ...
- K3BOM跳层
A自制件,B自制件,C外购件 ,结构为A-B-C 如果需要跳层,则设置A-B跳层,B-C跳层,则生成A计划订单,C计划订单, 假设单独A-B跳层,则MRP运算出的结果也是A计划订单,B计划订单,C计划 ...
- Java开发之@PostConstruct执行顺序
构造函数==>postConstruct==>init==destory==>predestory==卸载servlet;; 从Java EE5规范开始,Servlet增加了两个影响 ...
- java中的缓冲流!
package cn.zhozuohou; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- ES 6 系列 - Promise
一.含义 是异步编程的一种解决方案,es 6 将其变成了标准. 简单的说是一个容器,里面保存了某个未来才会结束的事件(通常是一个异步操作)的结果.语法上, Promise 是一个对象,从它可以获取异步 ...
- Windows & RabbitMQ:集群(clustering) & 高可用(HA)
描述:我们需要配置三台服务器:ServerA, ServerB, ServerC 注意事项: 所有的服务器的Erlang版本,RabbitMQ版本必须一样 服务器名大小写敏感 Step 1:安装Rab ...
- debug错误
Description "opt_design" can fail with error messages similar to the following: opt_design ...
- poj-1386(欧拉回路)
题意:给你n个单词,每个单词可以和另一个单词连接,前提是(这个单词的尾字母等下一个单词的首字母),问你有没有一种连法能够连接所有的单词: 解题思路:每个单词可以看成是首字母指向尾字母的一条边,那么就变 ...
- python3.7之12306抢票脚本实现
悲催的12306,彻底沦为各路抢票软件的服务提供方.元旦伊始,纯粹12306官网及APP抢票,愈一周的时间,仅到手一张凌晨3:55回家的站票.为远离脑残,无奈选择抢票软件,预购年后返沪车票.BTW,研 ...