ACM Hong Kong online game c. Classrooms (greedy)

Source: Internet
Author: User



Original title URL: https://open.kattis.com/problems/classrooms






Classrooms



The new semester is on to begin, and finding classrooms for orientation activities are always a headache.



there is K classrooms on campus and N proposed Activities that need to be assigned a venue. Every proposed activity has specfic starting time s i and ending time f i . Any such a activity should take place at one of the classrooms. Any of the K classrooms are big enough to hold any of the proposed activities, and all classroom can hold on most One activity at any time. No. Proposed activities can take place on the same classroom at the same time. Even if proposed activities overlap momentarily (the ending time of one activity equals the starting time another ACTI vity), they cannot is assigned to the same classroom.



There is so many proposed activities that there is not being enough classrooms to hold all the activities. It's desirable to has as many activities as possible. At the many proposed activities can is assigned to the classrooms?



Input


    • The first line contains the positive integers n and K (1≤kn≤200000), representing th E number of proposed activities and number of classrooms, respectively.
    • The following n lines each contains II positive integers:the ith line among these n lines con Tains si and Fi (1≤sifi≤109), indicating the starting time and ending time of proposed activity I


Output



Output An integer indicating the maximum number proposed activities, can be scheduled.


Sample Input 1

Sample Output 1

4 2

1 4

2 9

4 7

5 8

3


Author (s): Lau Lap Chi



Source: Hong Kong Regional Online Preliminary 2016









Test instructions, n activities, K classrooms, given the start and end times of each activity, there must be an interval between the end and start times of the two consecutive activities held in the same classroom. The maximum number of events that can be held.



Greedy, sort each activity by the end time and sweep it through.



The Multiset stores the end time of the ongoing activity in each classroom.



If the activity of a classroom in Multiset is completed before the start of activity I, activity I can be held, the original end time is deleted, and then the end time of the activity I is saved in.



If there is no end time in multiset smaller than A[i].begin, that is, the currently active classroom is not active until the start of activity I, the number of elements in the multiset indicates how many classrooms are active at the same time, and if there are empty classrooms, activity I can be carried out in this classroom, Deposit the end time of activity I into multiset.



Note: The actual deposit multiset is (-a[i].ed-1), and the search is used (-a[i].begin). Because the Lower_bound function is to be used, and Lower_bound (start,end,k) returns the index of the first number in the collection that is greater than or equal to K, and the title is looking for the first ending time that is smaller than the start time, a minus sign is just right.





#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath>

#define LL long long
#define N 200005
#define INF 0x3ffffff

Using namespace std;

Int n , m;
Struct node{
    Int bg, ed; //m start time and end time of each event
}a[N];

Bool cmp(node a , node b){ // sorted by end time
    If(a.ed== b.ed) return a.bg < b.bg;
    Return a.ed< b.ed;
}

Int main(){
    While (~scanf("%d%d" , &n , &m))
        {
            For (int i = 0 ; i < n ; ++i)
            Scanf("%d%d",&a[i].bg ,&a[i].ed);
            Sort(a,a+n,cmp);

            Multiset<int>endtime; //h stores the end time of the ongoing activity in each classroom
            Endtime.clear();
            Int ans = 0;
            For (int i = 0 ; i < n ; ++i){
                Multiset<int> :: iterator iter;
                Iter = endtime.lower_bound(-a[i].bg); //Whether there is a classroom activity that ends before the start time of i
                If (iter == endtime.end()){ //If there is no classroom that ends the activity before the event i starts, find another classroom
                    If (endtime.size() < m){
                        Endtime.insert(-a[i].ed- 1);
                        ++ans;
                    }
                    Continue;
                }
                Endtime.erase(iter); //Found that a classroom activity is over, activity i is in this classroom
                Endtime.insert( - a[i].ed - 1); //Update the end time of the event
                ++ans;
            }
        Printf("%d\n" , ans);
        }
} 





ACM Hong Kong online game c. Classrooms (greedy)


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.