Gym 101911A Coffee Break(优先队列)

Recently Monocarp got a job. His working day lasts exactly m minutes. During work, Monocarp wants to drink coffee at certain moments: there are n minutes a1,a2,…,an
, when he is able and willing to take a coffee break (for the sake of simplicity let’s consider that each coffee break lasts exactly one minute).

However, Monocarp’s boss doesn’t like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute ai, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least d minutes pass between any two coffee breaks. Monocarp also wants to take these n coffee breaks in a minimum possible number of working days (he doesn’t count days when he is not at work, and he doesn’t take coffee breaks on such days). Take into account that more than d minutes pass between the end of any working day and the start of the following working day.

For each of the n given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.
Input

The first line contains three integers n, m, d (1≤n≤2⋅105,n≤m≤109,1≤d≤m) — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.

The second line contains n distinct integers a1,a2,…,an (1≤ai≤m), where ai is some minute when Monocarp wants to have a coffee break.
Output

In the first line, write the minimum number of days required to make a coffee break in each of the n

given minutes.

In the second line, print n
space separated integers. The i-th of integers should be the index of the day during which Monocarp should have a coffee break at minute ai. Days are numbered from 1. If there are multiple optimal solutions, you may print any of them.
Examples
Input

4 5 3
3 5 1 2

Output

3
3 1 1 2

Input

10 10 1
10 5 7 4 6 3 2 1 9 8

Output

2
2 1 1 2 2 1 2 1 1 2
Note
In the first example, Monocarp can take two coffee breaks during the first day (during minutes 1 and 5, 3 minutes will pass between these breaks). One break during the second day (at minute 2), and one break during the third day (at minute 3).
In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
题目链接
参考题解

题目大意:有一位工作人员想利用喝咖啡来休息,所以他给了我们一个数组a,表示他想喝咖啡的时间点(假设她喝咖啡的时间不计),但是呢,他要是频繁喝咖啡老板就不同意了,所以每次喝咖啡的间隔要大于d。问他要将数组中的时间点都经历一边至少要多少天。并把每个时间点是在第几天喝咖啡的输出。
简单来说,就是将这个数组分成n段,每段中每两个数之差大于d。求n的最小值。

这个题目,我的第一反应是想到了以前做过的一个dp入门题目“导弹拦截系统”,可是写了好久,发现也不好写,还不过,emmm,就看了一下这个题解,很巧的用了优先队列。
首先,对可以喝咖啡的时间进行排序,用一个map记录在某个时间段喝咖啡是在那一天完成的。每次都先看看队首元素是不是要比现在这个时间点早,并且时间差大于d,如果是,那么就把队首时间点出队,将当前这个时间点标记为与刚刚队首元素同一天然后入队。如果不符合刚刚的条件,因为这是优先队列,那么如果与队首时间点差值都不够大,后面肯定也不符合,直接新开一天来喝这个时间点的咖啡,然后入队。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 2e5 + 5;
int origin[maxn], after[maxn];
map m;
priority_queue, greater > q;

int main()
{
    int n, mm, d, cnt = 1;
    cin >> n >> mm >> d;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &origin[i]);
        after[i] = origin[i];
    }
    sort(after + 1, after + 1 + n);
    m[after[1]] = 1;    q.push(1);
    for(int i = 2; i <= n; i++)
    {
        int top = q.top();
        if(after[i] - after[top] > d)
        {
            m[after[i]] = m[after[top]];
            q.pop();
        }
        else
        {
            cnt++;
            m[after[i]] = cnt;
        }
        q.push(i);
    }
    cout << cnt << endl;
    for(int i = 1; i <= n; i++)
        printf("%d ", m[origin[i]]);
    return 0;
}

你可能感兴趣的:(基础算法,图论)