poj3190 Stall Reservations

我一开始想用线段树,但是发现还要记录每头cow所在的棚......

无奈之下选择正解:贪心。

用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案。

注意代码实现的细节。

复制代码
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <queue>
 4 using namespace std;
 5 const int N = 50010;
 6 struct Cow {
 7     int a, b, num;
 8     bool operator < (const Cow& x) const {
 9         return this->a < x.a;
10     }
11     bool operator > (const Cow& x) const {
12         return this->a > x.a;
13     }
14 }c[N];
15 priority_queue< Cow, vector<Cow>, greater<Cow> >Q;
16 int ans[N];
17 int main() {
18     int n, top = 0;
19     scanf("%d", &n);
20     for(int i = 1; i <= n; i++) {
21         scanf("%d%d", &c[i].a, &c[i].b);
22         c[i].num = i;
23     }
24     sort(c + 1, c + n + 1);
25     Cow x;
26     x.a = c[1].b;
27     x.b = ++top;
28     ans[c[1].num] = top;
29     Q.push(x);
30     for(int i = 2; i <= n; i++) {
31         //printf("i:%d c[i].b:%d Q.top().a:%d\n", c[i].num, c[i].b, Q.top().a);
32         if(Q.top().a < c[i].a) {
33             x = Q.top();
34             Q.pop();
35             x.a = c[i].b;
36             ans[c[i].num] = x.b;
37             Q.push(x);
38         }
39         else {
40             x.a = c[i].b;
41             x.b = ++top;
42             ans[c[i].num] = top;
43             Q.push(x);
44         }
45     }
46     printf("%d\n", top);
47     for(int i = 1; i <= n; i++) {
48         printf("%d\n", ans[i]);
49     }
50     return 0;
51 }
AC代码
复制代码

 

posted @   huyufeifei  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡
试着放一个广告栏(虽然没有一分钱广告费)

『Flyable Heart 応援中!』 HHG 高苗京铃 闪十PSS 双六 電動伝奇堂 章鱼罐头制作组 はきか 祝姬 星降夜
点击右上角即可分享
微信分享提示