HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 解读

有一组数据是在客人到达和出发时间,问:多少把椅子的能力,以满足所有客人的需求,可以有一个地方坐下要求。

有些人甚至开始考虑暴力法,这些数据是少,其实这个问题很多数据, 暴力需求O(n*n)的时间效率,显然,将加班,因此,有必要O(n) 或O(nlgn)算法。

属于一道想透了就很easy的,可是没想过就会很困难的题目。

解法是:

把全部客人到来和离开的时间都排成序列。每次客人到来须要n张桌椅,那么就+上n,每次客人离开就会返还n张桌椅,那么就-去n,求这种最大值。

详细算法代码就那么几行,处理IO的代码都比这个长。

要想出来还真是十分困难。只是也算是经典算法了,应该学熟练。

const int MAX_N = 10001;
struct Interval
{
	int t, wei;
	bool operator<(Interval const &i) const
	{
		if (t == i.t) return wei < i.wei;
		return t < i.t;
	}
};

Interval inter[MAX_N<<1];

inline int timeToSec(int h, int m)
{
	return h * 60 + m;
}

int main()
{
	int T, n, h, m, w, num;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		num = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d %d:%d", &w, &h, &m);
			inter[num].t = timeToSec(h, m);
			inter[num++].wei = w;

			scanf("%d:%d", &h, &m);
			inter[num].t = timeToSec(h, m);
			inter[num++].wei = -w;
		}
		sort(inter, inter+num);
		int ans = 0, tmp = 0;
		for (int i = 0; i < num; i++)
		{
			tmp += inter[i].wei;
			ans = max(tmp, ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-10-09 12:53:12

HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 解读的相关文章

HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 题解

有一组数据是客人到来和离开的时间,问需要多少张桌椅才能满足所有客人都能有位置坐的要求. 有人居然一开始就想到暴力法,以为数据量少,其实本题数据量不少了, 暴力法需要O(n*n)的时间效率了,显然是会超时的,故此需要O(n) 或者O(lgn)的算法. 属于一道想透了就非常容易的,但是没想过就会非常困难的题目. 解法是: 把所有客人到来和离开的时间都排成序列,每次客人到来需要n张桌椅,那么就+上n,每次客人离开就会返还n张桌椅,那么就-去n,求这样的最大值. 具体算法代码就那么几行,处理IO的代码都

hdu 4883 TIANKENG’s restaurant(暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come t

HDU 4883 TIANKENG’s restaurant

Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

HDU 4883 TIANKENG’s restaurant(区间选点)

HDU 4883 TIANKENG's restaurant 题目链接 题意:给定一些时间作为区间,和一个人数,问要安排多少人去看管(一个人只能看管一个人) 思路:普通的区间选点问题,一个区间拆成一个进入点一个出去点,然后排序循环求答案即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 20005; struct Man {

HDU 4883 TIANKENG’s restaurant (贪心)

链接:带我学习,带我飞 第一次BC,稳挂,WA n多次,今天重新做了一下 略挫 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <string> #include <vector> #include <set> #include <algorithm>

HDU 4883 TIANKENG’s restaurant (区间更新)

Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the

HDU 4883 (BC#2 1001题)TIANKENG’s restaurant(水)

题目地址:HDU 4883 唉..最近得了一种惯性..刚学了什么就碰到个类似的就想用刚学的算法...原来这题如此简单..当时真是想多了.... 只要在需要变化的点上设置一个值,是增多少还是减多少.然后当遍历过来的时候加上或减去这个值就行了... 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

[ACM] HDU 4883 TIANKENG’s restaurant

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 931    Accepted Submission(s): 412 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens