题目
一段路上,给出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;
}