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 ...
随机推荐
- ssl证书部署问题
问:我现在得到的ssl证书是.crt和.key两个在nginx环境下部署的证书,如果我们改用是tomcat,现在把这两个文件合成了.jks给tomcat使用,合成的时候输入的jks密码是不是就是部署在 ...
- PHP的特质Trait使用
参考: Trait的使用,网站地址https://www.jianshu.com/p/fc053b2d7fd1
- 老男孩python学习自修第七天【包与模块】
1.如何导入 from package import module module.function() 常用魔术方法 __init__.py 如果某个文件夹下面有该文件,则该文件夹是一个包,否则只是一 ...
- js正則表達式
正則表達式實例化的兩種方式: 字符型 var a=// 對象型var a=new RegExp(,) 修飾符: i:忽略大小寫 g:全局搜索 m:多行搜索 元字符: \轉義字符 \w:字符,數字,下劃 ...
- php2
session //将用户的会话数据存储在服务端,通过 session_start()开启session,通过$_SESSION读写session session_start(); //开启ses ...
- Linux下4个查找命令which、whereis、locate、find的总结
(1)which [-a] cmdname1 cmdname2 ...... 作用:locate a command,从环境变量PATH中,定位/返回与指定名字相匹配的可执行文件所在的路径 ...
- python----re正则模块详解
今天介绍一下Python中常用的正则表达式处理函数.Python的正则表达式主要有两种方法完成模式匹配:『搜索』和『匹配』 re.match re.match 尝试从字符串的开始全部或者部分匹配某个模 ...
- Typecho——简介及安装
Typecho Typecho是由type和echo两个词合成的,来自于开发团队的头脑风暴.Typecho基于PHP5开发,支持多种数据库,是一款内核强健﹑扩展方便﹑体验友好﹑运行流畅的轻量级开源博客 ...
- Android ProgressDialog 简单实用
ProgressDialog progressDialog; @SuppressLint("HandlerLeak") Handler handler1 = new Handler ...
- 查询SQLSERVER中系统所有表
SQL 查询所有表名: SELECT NAME FROM SYSOBJECTS WHERE TYPE='U' SELECT * FROM INFORMATION_SCHEMA.TABLES 查询表的所 ...