NYOJ 14 venue arrangement problem (also a classic problem)

Source: Internet
Author: User
Venue schedule problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 4
Description
There are many activities in the auditorium of the school every day. The scheduled time for these activities may conflict, and you need to select some activities to hold. Xiao Liu's job is to arrange activities in the school auditorium. At most one activity can be arranged at a time. Now Mr. Liu has some schedules for his activities. He wants to arrange as many activities as possible. How can he arrange them.
Input
The first row is an integer m (m <100), indicating a total of m groups of test data.
The first row of test data in each group is an integer n (1 <n <10000), indicating that the test data has n activities.
Next n rows, each row has two positive integers Bi, Ei (0 <= Bi, Ei <10000), indicating the start time and end time of the I-th activity (Bi <= Ei)
Output
For each group of inputs, the maximum number of activities that can be arranged is output.
Output of each group occupies one row
Sample input
221 1010 1131 1010 1111 20
Sample output
12
Prompt

Note: If the last activity ends at t time, the next activity should start at t + 1 at the earliest.


Algorithm analysis:

There are three kinds of algorithms that can be considered for scheduling without overlapping intervals:

1. In the optional work, the earliest end time is selected each time.

2. In the optional work, the shortest job is selected every time.

3. In an optional job, work that overlaps with the minimum number of optional jobs is selected each time.

Facts have proved that only one of them is correct and can withstand the test.


For the first algorithm, the final end time of the algorithm when the same number of jobs started earlier is not later than that of other schemes.

That is to say, the number after the first ending interval must be greater than or equal to the number of other later intervals.


For details, see: http://blog.csdn.net/luoweifu/article/details/18195607

The code is as follows:

# Include <iostream> # include <utility> # include <algorithm> using namespace std; typedef pair <int, int> name; name arr [10005]; int cmp (name, name B) {return. second <B. second;} int main () {int N, n, I, ans, t; cin> N; while (N --) {cin> n; for (I = 0; I <n; I ++) cin> arr [I]. first> arr [I]. second; sort (arr, arr + n, cmp); ans = 0, t =-1; for (I = 0; I <n; I ++) {if (t <arr [I]. first) {ans ++; t = arr [I]. second ;}} cout <ans <endl ;}return 0 ;}

Because sort sorts the pair type first by default .. I realized it only after WA.

NYOJ 14 venue arrangement problem (also a classic problem)

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.