牛挤奶

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

  这一题如果把时间区间去掉,那就变成装箱子问题了(装箱子要用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. php复习

    最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...

  2. BZOJ-1901 Zju2112 Dynamic Rankings 函数式线段树 套 树状数组+离线处理

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MB Submit: 6058 Solved: 2521 [Su ...

  3. 【bzoj1013】 JSOI2008—球形空间产生器sphere

    www.lydsy.com/JudgeOnline/problem.php?id=1013 (题目链接) 题意 有一个n维的球体,给出球上n+1个点,求出圆心. Solution 题中给出了对于n维空 ...

  4. Request获取信息总结

    Request.ServerVariables("REMOTE_ADDR")          '获取访问IPRequest.ServerVariables("LOCAL ...

  5. BZOJ2654 tree

    Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...

  6. jsp学习(五)

    在进行jsp与jdbc连接时,出现这样一个错误,提示如下: java.net.ConnectException: Connection refused: connect 后来发现是由于mysql数据库 ...

  7. 排序算法(一)(时间复杂度均为O(n*n))

    对于一个int数组,请编写一个选择排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] 冒泡排序 ...

  8. MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法

    MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法 1 问题 [root@localhost m ...

  9. xampp 安装red扩展出错解决

    Linux Mint + Xampp Error + ‘grep: /opt/lampp/include/php/main/php.h: No Such File Or Directory’ FEBR ...

  10. CodeForces 701A Cards

    直接看示例输入输出+提示 1. 统计所有数的和 sum,然后求 sum/(n/2) 的到一半数的平均值 6 1 5 7 4 4 3 ->1+5+7+4+4+3=24  分成3组 每组值为8 in ...