CF1041C Coffee Break

CF1041C Coffee Break

題目大意:

給定nn個數和一個kk,這nn個數都不超過mmhtml

每次從沒被去掉的數裏面選一個數aa,去掉aa,而後能夠任意一個b(b>a+k)b(b>a+k),而後去掉任意一個c(c>b+k)c(c>b+k),以此類推node

問最少能選多少個aa,而後輸出每一個數都是選第幾個aa的時候被去掉的ios

輸入格式:

一行三個整數n,m,k
app

再一行nn個整數,表示給定的數ide

輸出格式:

第一行一個整數,表示最少選aa的個數ui

第二行nn個整數,表示每一個數都是選第幾個aa時被去掉的this

題目描述

Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are nn minutes a_1, a_2, \dots, a_na1,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).spa

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 a_iai , Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least dd minutes pass between any two coffee breaks. Monocarp also wants to take these nn 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 dd minutes pass between the end of any working day and the start of the following working day.code

For each of the nn 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.htm

輸入輸出格式

輸入格式:

 

The first line contains three integers nn , mm , d(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)(1n2105,nm109,1dm)— 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 nn distinct integers a_1, a_2, \dots, a_na1,a2,,an (1 \le a_i \le m)(1aim) , where a_iai is some minute when Monocarp wants to have a coffee break.

 

輸出格式:

 

In the first line, write the minimum number of days required to make a coffee break in each of the nn given minutes.

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

 

輸入輸出樣例

輸入樣例#1:  複製
4 5 3
3 5 1 2
輸出樣例#1:  複製
3
3 1 1 2 
輸入樣例#2:  複製
10 10 1
10 5 7 4 6 3 2 1 9 8
輸出樣例#2:  複製
2
2 1 1 2 2 1 2 1 1 2 
/*
    很顯然是一個貪心,從左到右查找第一個時間點,而後以此爲起點,向後儘可能多的刪去其餘時間點
    當知道a時間點時,咱們要求的是知足b>a+k的最小的b,因而能夠用二分查找找到b的位置,而後作標記 
*/ 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int b[200010];
int n,m,d,k,num,pre,Pre,Num;
struct node{
    int val,id,tag,bel;
    bool operator < (const node w)const{
        return val<w.val;
    }
    node(){tag=0;}
}a[200010];
int Search(int x){
    int l=x,r=n,mid,ans=n+1;
    while(l<=r){
        mid=(l+r)>>1;
        if(a[mid].val>a[x].val+d)ans=mid,r=mid-1;
        else l=mid+1;
    }
    for(int i=ans;i<=n;i++)
        if(!a[i].tag)return i;
    return n+1;
}
int main(){
    scanf("%d%d%d",&n,&m,&d);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i].val),a[i].id=i;
    Pre=1;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)b[a[i].id]=i;
    while(k!=n){
        num++;
        for(int i=Pre;i<=n;i++)
            if(!a[i].tag){
                pre=i;Pre=pre+1;
                break;
            }
        a[pre].tag=1;k++;a[pre].bel=num;
        while(1){
            Num=Search(pre);
            if(Num>n)break;
            a[Num].bel=num;
            a[Num].tag=1;
            k++;pre=Num;
        }
    }
    printf("%d\n",num);
    for(int i=1;i<=n;i++)
        printf("%d ",a[b[i]].bel);
    return 0;
}
相關文章
相關標籤/搜索