其他分享
首页 > 其他分享> > AcWing 畜栏预定

AcWing 畜栏预定

作者:互联网

AcWing 畜栏预定

Description

Input

Output

Data Size

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

题解:

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#define N 50005
using namespace std;

struct Node
{
    int l, r, id;
    friend bool operator < (Node x, Node y) {
        return x.r > y.r;
    }
};
priority_queue<Node> que;
struct A {int l, r, id, ans;} a[N];
int n, cnt;

bool cmp1(A x, A y) {return x.l < y.l;}
bool cmp2(A x, A y) {return x.id < y.id;}

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' ||c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
        a[i].l = read(), a[i].r = read(), a[i].id = i;
    sort(a + 1, a + 1 + n, cmp1);
    que.push((Node){a[1].l, a[1].r, ++cnt});
    a[1].ans = cnt;
    for(int i = 2; i <= n; i++)
    {
        if(que.top().r < a[i].l)
        {
            a[i].ans = que.top().id;
            que.pop();
            que.push((Node){a[i].l, a[i].r, a[i].ans});
        }
        else
        {
            a[i].ans = ++cnt;
            que.push((Node){a[i].l, a[i].r, cnt});
        }
    }
    cout << cnt << endl;
    sort(a + 1, a + 1 + n, cmp2);
    for(int i = 1; i <= n; i++) printf("%d\n", a[i].ans);
    return 0;
}

标签:include,头牛,int,预定,畜栏,奶牛,id,AcWing
来源: https://www.cnblogs.com/BigYellowDog/p/11281603.html