HDU 4883 Best Coder Round 2 Tiankeng ' s restaurant interpretation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.