51Nod event scheduling problem (sorting + priority queue)

There are several activities, the ith start time and end time are [Si, fi), and the activities arranged in the same classroom cannot overlap. To arrange all activities, how many classrooms are required at least? 

Input
A positive integer n (n <= 10000) in the first line represents the number of activities.
Lines 2 to (n + 1) contain n start and end times.
The start time is strictly less than the end time, and the times are all non-negative integers less than 1000000000
Output
A line contains an integer representing the minimum number of classrooms.
Input example
3
1 2
3 4
2 9
Output example
2
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
struct node
{
    ll u,v;
    bool operator<(const node a)
    {
        return a.u==u?a.v>v:a.u>u;
    }
}e[10006];
int n;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lld%lld",&e[i].u,&e[i].v);
    sort(e,e + n);
    priority_queue<int,vector<int>,greater<int> >q;
    ll end=e[0].v,pos=1;
    q.push(e[0].v);
    for(int i=1;i<n;i++)
    {
        if(e[i].u<q.top()) pos++;
        else q.pop();
        q.push(e[i].v);
    }
    printf("%lld\n",pos);
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325070724&siteId=291194637