Greedy:Stall Reservations(POJ 3190)

题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚?
这一题如果把时间区间去掉,那就变成装箱子问题了(装箱子要用Splay维护),但是现在规定了区间和时间,我们只用贪婪算法就可以了,每次只用找到最小就可以了(用堆维护)。
PS:一开始我想到用AVL去维护,的都不知道我在想什么,简直浪费时间
#include <iostream>
#include <functional>
#include <algorithm> using namespace std; typedef struct iterval
{
int cows_num;
int Which_Stall;
int start;
int end;
}Cow;
typedef struct box
{
int box_num;
int min_t;
}Stall;
typedef int Position;
int fcomp1(const void *a, const void *b)
{
return (*(Cow *)a).start - (*(Cow *)b).start;
}
int fcomp2(const void *a, const void *b)
{
return (*(Cow *)a).cows_num - (*(Cow *)b).cows_num;
} static Cow cows_set[];
static Stall s_heap[];
static int sum_of_stalls; void Search(const int);
void Insert(Stall, Position);
Stall Delete_Min(void); int main(void)//这一题用堆维护
{
int n;
while (~scanf("%d", &n))
{
for (int i = ; i < n; i++)
{
scanf("%d%d", &cows_set[i].start, &cows_set[i].end);
cows_set[i].cows_num = i;
}
qsort(cows_set, n, sizeof(Cow), fcomp1);
Search(n);
}
return ;
} void Insert(Stall goal, Position pos)
{
Position s = pos, pr; for (; s > ; s = pr)//上滤
{
pr = s % == ? s >> : (s - ) >> ;
if (s_heap[pr].min_t > goal.min_t)
s_heap[s] = s_heap[pr];
else break;
}
s_heap[s] = goal;
} Stall Delete_Min(void)
{
Stall mins_stalls = s_heap[],tmp = s_heap[sum_of_stalls--];
Position pr, s1, s2; for (pr = ; pr <= sum_of_stalls;)
{
s1 = pr << ; s2 = s1 + ;
if (s2 <= sum_of_stalls)
{
if (s_heap[s1].min_t < s_heap[s2].min_t){
s_heap[pr] = s_heap[s1]; pr = s1;
}
else{
s_heap[pr] = s_heap[s2]; pr = s2;
}
}
else
{
if (s1 <= sum_of_stalls){
s_heap[pr] = s_heap[s1]; pr = s1;
}
break;
}
}
Insert(tmp, pr);
return mins_stalls;
} void Search(const int n)
{
Stall tmp;
sum_of_stalls = ;
tmp.box_num = ; tmp.min_t = cows_set[].end;
Insert(tmp, );
cows_set[].Which_Stall = ; for (int i = ; i < n; i++)
{
if (cows_set[i].start <= s_heap[].min_t)//放不下
{
tmp.box_num = ++sum_of_stalls; tmp.min_t = cows_set[i].end;
Insert(tmp, sum_of_stalls);
cows_set[i].Which_Stall = sum_of_stalls;
}
else
{
tmp = Delete_Min();
tmp.min_t = cows_set[i].end;
cows_set[i].Which_Stall = tmp.box_num;
Insert(tmp, ++sum_of_stalls);
}
}
printf("%d\n", sum_of_stalls);
qsort(cows_set, n, sizeof(Cow), fcomp2);
for (int i = ; i < n; i++)
printf("%d\n", cows_set[i].Which_Stall);
}

Greedy:Stall Reservations(POJ 3190)的更多相关文章
- Stall Reservations(POJ 3190 贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4434 Accepted: 158 ...
- Stall Reservations POJ - 3190 (贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11002 Accepted: 38 ...
- Stall Reservations POJ - 3190(贪心)
Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- python 学习5--matplotlib画图实践
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...
- BZOJ-1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法+乱搞
1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 1383 Solved: 7 ...
- Rebar:Erlang构建工具
Rebar是一款Erlang的构建工具,使用它可以方便的编译.测试erlang程序.内联驱动和打包Erlang发行版本. Rebar是一个独立的erlang脚本,所以使用Rebar发布程序非常简单,甚 ...
- 看看这些JavaScript题目你会做吗?
题目1 咋一看这题目,还以为答案选择B呢,其实正确答案为D,知道原因吗?接着往下看 map对数组的每个元素调用定义的回调函数并返回包含结果的数组,咋一看还以为它会像如下这样执行: function t ...
- Mac OS下编写对拍程序
介绍 对拍是信息学竞赛中重要的技巧,它通过一个效率低下但正确率可以保证的程序,利用庞大的随机生成数据来验证我们的高级算法程序.对拍最大的优势在于可以通过人力所不能及的速度和数量达到验证的效果.下面我们 ...
- UVA11400照明系统设计&& POJ1260Peals(DP)
紫书P275: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/A POJ http://poj.org/pr ...
- 可输入自动匹配Select——jquery ui autocomplete
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Hadoop的datanode无法启动
Hadoop的datanode无法启动 hdfs-site中配置的dfs.data.dir为/usr/local/hadoop/hdfs/data 用bin/hadoop start-all.sh启动 ...
- Mutex和内存可见性
http://ifeve.com/mutex-and-memory-visibility/ POSIX内存可见性规则 IEEE 1003.1-2008定义了XBD 4.11内存同步中的内存可见性规则. ...
- TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
/** * * @author ocq */ class Parent implements Comparable { private int age = 0; public Parent(int a ...