牛挤奶

  题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚?

  这一题如果把时间区间去掉,那就变成装箱子问题了(装箱子要用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)的更多相关文章

  1. Stall Reservations(POJ 3190 贪心+优先队列)

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 158 ...

  2. Stall Reservations POJ - 3190 (贪心+优先队列)

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11002   Accepted: 38 ...

  3. Stall Reservations POJ - 3190(贪心)

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...

  4. poj 3190 Stall Reservations

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

  5. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  6. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  7. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  8. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

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

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

随机推荐

  1. cogs896 圈奶牛

    描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...

  2. IOS基础之 (七) 分类Category

    一 Category 分类:Category(类目,类别) (OC有) 命名:原来的类+类别名(原来的类名自动生成,只要写后面的类别名,一般以模块名为名.比如原来类 Person,新建分类 Ct,新建 ...

  3. POJ1651Multiplication Puzzle(矩阵链乘变形)

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8040   Accepted: ...

  4. POJ2676Sudoku(类似于八皇后)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16444   Accepted: 8035   Special ...

  5. sdk和ndk

    让我先来说说android sdk (Android Software Development Kit, 即Android软件开发工具包)可以说只要你使用java去开发Android这个东西就必须用到 ...

  6. tp框架查询

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...

  7. How much training data do you need?

    How much training data do you need?   //@樵夫上校: 0. 经验上,10X规则(训练数据是模型参数量的10倍)适用与大多数模型,包括shallow networ ...

  8. 锋利的jQuery-4--阻止事件冒泡和阻止默认行为

    阻止事件冒泡: 如果嵌套元素分别有自己的click事件,当点击内层元素时外层元素的事件也会被触发. $("span").bind("click", functi ...

  9. ecshop 在php5.5上安装错误解决

    1.找到ecshop\includes\cls_image.php文件 搜索 function gd_version 改成 static function gd_version 2.Strict St ...

  10. Apache22中配置虚拟主机(Apache VirtualHost)

    Apache VirtualHost的作用就是可以让一个apache为多个域名服务,相当于一个服务器挂了N多个网站,举个例子: 我的apache服务器,ip为x.x.x.x,我有两个域名www.too ...