我一开始想用线段树,但是发现还要记录每头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的更多相关文章

  1. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  2. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

  3. poj3190 Stall Reservations(贪心+STL)

    https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...

  4. poj3190 Stall Reservations (贪心+优先队列)

    Cleaning Shifts Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  5. POJ3190 Stall Reservations 贪心

    这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...

  6. 【POJ - 3190 】Stall Reservations(贪心+优先队列)

    Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...

  7. poj 3190 Stall Reservations

    http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  8. poj3190 stall revertation

                                                                                                Stall Re ...

  9. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

随机推荐

  1. Oracle分析函数row_number()等的使用实例

    --分析函数 --rank() over(order by) --值相同,排名相同,序号跳跃 select * from t_account select rank() over(order by u ...

  2. AngularJS集合数据遍历显示

    AngularJS集合数据遍历显示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  3. vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址

    一.开发环境中跨域 使用 Vue-cli 创建的项目,开发地址是 localhost:8080,需要访问非本机上的接口http://10.1.0.34:8000/queryRole.不同域名之间的访问 ...

  4. 微信小程序自定义组件

    要做自定义组件,我们先定一个小目标,比如说我们在小程序中实现一下 WEUI 中的弹窗组件,基本效果图如下. Step1 我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建 ...

  5. vue前端框架面试问题汇总

    1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...

  6. java学习之—队列

    /** * 队列 * Create by Administrator * 2018/6/11 0011 * 下午 3:27 **/ public class Queue { private int m ...

  7. 将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)

    今天在生成数据的时候遇到了这个需求,其实写一个for循环可以很容易解决这个问题,但是无论是性能还是酷炫程度上都不行 所以顺手搜索了一下. 例子是将 l = [[1, 2, 3], [4, 5, 6], ...

  8. python学习笔记(4)-基本数据类型-数字类型及操作

    大学mooc 北京理工大学 python语言程序设计课程学习笔记 一.整数类型 可正可负,没有取值范围的限制(这个与c不同,c要考虑数据类型的存储空间).如pow(x,y),计算x的y次方,pow(2 ...

  9. how to build an app with github

    how to build an app with github Building apps https://developer.github.com/apps/ demos https://githu ...

  10. EChart.js 笔记一

    一直对数据可视化比较感兴趣,当年 Alibaba 年报晚会上的大屏显示可谓是技惊四座,够震撼,将数据之美展现得淋漓尽致. 国内的前端数据可视化插件中,echart.js 算是热度很高的,也容易上手,算 ...