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 ...
随机推荐
- VSCode 汉化
https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html
- hadoop分布式系统架构详解
hadoop 简单来说就是用 java写的分布式 ,处理大数据的框架,主要思想是 “分组合并” 思想. 分组:比如 有一个大型数据,那么他就会将这个数据按照算法分成多份,每份存储在 从属主机上,并且在 ...
- python爬虫之MongoDB测试环境安装
一. 下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二. 安装 1>设置mongoDB ...
- CDH 6.0.1 集群搭建 「Process」
这次搭建我使用的机器 os 是 Centos7.4 RH 系的下面以流的方式纪录搭建过程以及注意事项 Step1: 配置域名相关,因为只有三台机器组集群,所以直接使用了 hosts 的方法: 修改主机 ...
- SpringMVC配置三大组件
1.组件扫描器 使用组件扫描器省去在spring容器配置每个Controller类的繁琐. 使用<context:component-scan>自动扫描标记@Controller的控制器类 ...
- Centos rpm包安装PHP所需包
yum -y install php php-devel php-fpm php-xml php-pdo php-ldap php-mysql
- JS检测是否是360浏览器
// JavaScript Document //application/vnd.chromium.remoting-viewer 可能为360特有 var is360 = _mime("t ...
- Python自动化运维ansible从入门到精通
1. 下载安装 在windows下安装ansible:
- MySQL——基础操作
参考博客:http://www.cnblogs.com/wupeiqi/articles/5713315.html 1.创建用户.授权(默认root,密码为空) 创建: create user 'al ...
- Xamarin 简化的Android密钥库签名
安装 开始使用这个新工具不容易.在Visual Studio 2017(即将推出VS 2015),只需转到工具 - >扩展和更新,并搜索“密钥库”来查找扩展名. 下载后,只需重新启动Visual ...