HDU 4883 Best Coder Round 2 TIANKENG's restaurant question
There is a set of data about the arrival and departure time of the guests. Ask how many tables and chairs are required to meet the requirements of all the guests.
Some people thought of the violence law at the beginning and thought that there was a small amount of data. In fact, there was a large amount of data in this question, and the violence law required O (n * n) time efficiency. Obviously, it would time out, therefore, we need O (n) or O (lgn) algorithms.
It is very easy to think about it, but it is very difficult to think about it.
Solution:
Arrange the arrival and departure times of all the guests in sequence. Each time the guests arrive, they need n tables and chairs, plus n. Each time the guests leave, they will return n tables and chairs, then-go to n and find the maximum value.
The specific algorithm code is just a few lines, and the IO processing code is longer than this. It is really difficult to come out, but it is also a classic algorithm and should be skillful.
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;}