[贪心] UVa10148 广告牌 (区间选点问题)

题目

一段路上,给出n个慢跑者跑步的区间,给出k,要求让每个慢跑者都能看到k个广告,区间都是整数操作,也就是说一个广告只能放在一个整数上,求最小贴的广告数

思路

区间选点问题的方法:
这里写图片描述

代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int maxG = 20000 + 1000;
const int Gd = 10000;
const int maxn = 1000 + 100;
int n, k, G[maxG];
vector<int> ans;
struct node {
    int x, y;
    bool operator < (const struct node& rhs) const {
        return y < rhs.y;
    }
}A[maxn];

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &k, &n);
        int a, b;
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &a, &b);
            A[i].x = min(a, b);
            A[i].y = max(a, b);
        }
        sort(A, A + n);
        ans.clear();
        memset(G, 0, sizeof(G));

        for (int x = 0; x < n; x++) {
            int cnt = k;
            for (int i = A[x].x; i <= A[x].y; i++)
                if (G[i + Gd]) cnt--;
            for (int py = A[x].y;py>=A[x].x; py--) {
                if (cnt <= 0) break;
                if (G[py + Gd]) continue;
                G[py + Gd] = 1;
                cnt--;
                ans.push_back(py);
            }
        }
        sort(ans.begin(),ans.end());
        printf("%d\n", ans.size());
        for (vector<int>::iterator iter = ans.begin(); iter != ans.end(); ++iter)
            printf("%d\n", *iter);

        if(T) printf("\n");

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值