HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 题解
来源:互联网 发布:初开淘宝店卖什么好 编辑:程序博客网 时间:2024/05/18 03:30
有一组数据是客人到来和离开的时间,问需要多少张桌椅才能满足所有客人都能有位置坐的要求。
有人居然一开始就想到暴力法,以为数据量少,其实本题数据量不少了, 暴力法需要O(n*n)的时间效率了,显然是会超时的,故此需要O(n) 或者O(nlgn)的算法。
属于一道想透了就非常容易的,但是没想过就会非常困难的题目。
解法是:
把所有客人到来和离开的时间都排成序列,每次客人到来需要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;}
1 0
- HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 题解
- hdu 4883 (Best Coder) TIANKENG’s restaurant
- hdu-4883- (Best Coder) TIANKENG’s restaurant
- hdu 4883 bestcoder round 2 1001 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- HDU-4883-TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant(排序或优先队列模拟)——BestCoder Round #2
- BestCoder Round #2 1001 TIANKENG’s restaurant
- BestCoder Round #2-TIANKENG’s restaurant
- BestCoder Round #2 1001TIANKENG’s restaurant
- hdu TIANKENG’s restaurant
- V7 jar
- 【RPi树莓派使用指南】GPIO驱动方法
- UVA 10228 - Star not a Tree?(模拟退火)
- Nested Classes,Static Classes,Non-static Classes,Inner Classes,Local Classes,Anonymous Classes
- Android:实现手势滑动的事件处理方法
- HDU 4883 Best Coder Round 2 TIANKENG’s restaurant 题解
- JavaScript中跨域调用Flash的方法
- hdu 3473划分树~~求中间值
- Til the Cows Come Home poj 2387 c++
- repair filesystem#
- NYOJ 题目42 一笔画问题 (欧拉连通图+并查集)
- HDU1008-Elevator
- hdu 1907(尼姆博弈)
- YCSB简介