HDU 4883 Best Coder Round 2 TIANKENG's restaurant question

Source: Internet
Author: User

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;}



Related Article

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.