Codeforces Round #509 (Div. 2) - C - Coffee Break(Set 二分)

5 篇文章 0 订阅
3 篇文章 0 订阅

Codeforces Round #509 (Div. 2) - C - Coffee Break

 题意:

有 n 个不同的休息时间,每天工作时间为m,任意两个休息点之间至少要隔d个时间,求最少工作天数,使得 n 个休息时间都休息了。

两天的休息点不会相互影响,因为题目中说到两天之间的时间默认 > d

对于每次设置休息点,就是记录一个上次休息时间 t +至少工作时间 d 为 pre,找到剩余的休息时间中最小的 > pre 的值,如果不存在则天数++,然后设置 pre = 0,然后继续找,直到所有的休息时间都被设置了。

查找的部分可以利用二分,但因为被设置过的时间要被移除,利用数组二分的话要更新数组(即数组下推),那样太慢了,考虑set的移除操作(大概logn),对于每个休息时间都只用一次二分查找和删除的操作,总的时间复杂度大概为(nlogn)。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, m, d, a[N];
set<int>b;
set<int>::iterator it;
int main()
{
    map<int, int> mp;
    scanf("%d%d%d", &n, &m, &d);
    for(int i=1;i<=n;i++) {
        scanf("%d", &a[i]); b.insert(a[i]);
    }
    int tot = 1, now = 1, ans = 0, sz = n;
    while(sz) {
        it = b.lower_bound(now);
        if(it == b.end()) {now = 1; tot ++; continue;}
        mp[*it] = tot; ans = tot;
        now = *it + d + 1; if(now > m) now = 1, tot ++;
        b.erase(*it); sz --;
    }
    printf("%d\n", ans);
    for(int i=1;i<=n;i++) {
        if(i-1) printf(" ");
        printf("%d", mp[a[i]]);
    }
    printf("\n");
    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值