There is a set of data that is available at the guest arrival and departure times, Q: How many chairs are required to meet the needs of all guests, there can be a place to sit down.
Some people even start to consider the violence law, which is less, in fact, a lot of data on this problem, violent demand O (n*n) time efficiency, obviously, will be overtime, therefore, it is necessary O (n) or O (NLGN) algorithm.
Belong to a thought through the very easy, but did not think it will be very difficult topic.
The solution is:
The time of arrival and departure of all guests is arranged in sequence. Every time a guest arrives to need n tables and chairs, then + on N, each time the guest leaves will return n tables and chairs, then-go N, ask this maximum value.
Detailed algorithm code in just a few lines, processing IO code is longer than this.
It's really hard to figure out. But also is a classical algorithm, should learn proficiency.
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;}};i Nterval inter[max_n<<1];inline int timetosec (int h, int m) {return h * + 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, & AMP;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;}
HDU 4883 Best Coder Round 2 Tiankeng ' s restaurant interpretation