CF1041C Coffee Break

Posted thmyl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1041C Coffee Break相关的知识,希望对你有一定的参考价值。

CF1041C Coffee Break

题目大意:

给定nn个数和一个kk,这nn个数都不超过mm

每次从没被去掉的数里面选一个数aa,去掉aa,然后可以任意一个b(b>a+k)b(b>a+k),然后去掉任意一个c(c>b+k)c(c>b+k),以此类推

问最少能选多少个aa,然后输出每个数都是选第几个aa的时候被去掉的

输入格式:

一行三个整数n,m,k

再一行nn个整数,表示给定的数

输出格式:

第一行一个整数,表示最少选aa的个数

第二行nn个整数,表示每个数都是选第几个aa时被去掉的

题目描述

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).

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.

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.

输入输出格式

输入格式:

 

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)(1ai?m) , 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;

 

以上是关于CF1041C Coffee Break的主要内容,如果未能解决你的问题,请参考以下文章

题解 CF1290D Coffee Varieties (hard version)

CF Gym-101911A Coffee Break

在生产中使用“coffee”而不是“node”命令

如何将 JSON 转换为 CoffeeScript 并写入文件“.coffee”?

文件方式实现完整的英文词频统计实例

通过require in循环调用多个描述