题解:Cuisine
题目描述
给定一些区间,你可以将每个区间放入任意一个块中,你要使得最终每一个块中任意两个区间的交集为空。求最大放入的区间数量。
思路
本题为基础贪心题。
首先,对于每道菜,有一个固定的烹饪时间和一个点火系数 ,因此,我们可以直接求出这道菜占用的时间。
于是题目转换为了一个区间覆盖问题,你要选出尽可能多的区间,使得任意两个区间交集为空。
由于所有菜从烹饪时间变为占用时间时,右端点是不变的,因此我们可以考虑以下的贪心做法。
将所有区间按照右端点升序排序,如果右端点相同的再按照左端点升序排序。
维护一个优先队列,队列中有 个元素,表示每个炉灶的最后一次被使用的时间。
依次处理每个区间,从优先队列中取出最小的元素,区间能接到当前这个炉灶的后边,就把当前区间接上,否则就是不能的,接着处理下一个区间。
这样可以处理出一次询问,如果要同时回答多个询问呢?
考虑将所有完成的时间按照顺序存储到数组中。对于每个询问,二分找到最大的满足条件的数量即可。
时间复杂度 。
Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
#include <utility>
#include <ctime>
#define ll long long
using namespace std;
template<typename T> inline void read(T& x) {
x = 0; T f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x *= f;
}
const int N = 1e6 + 5;
int n, m;
int ans[N], cnt;
double u;
struct cuisine {
int l, r;
bool operator < (const cuisine &rhs) const {
if (r != rhs.r) return r < rhs.r;
return l < rhs.l;
}
} a[N];
int main() {
read(n); read(m);
scanf("%lf", &u);
for (int i = 1; i <= m; ++i) {
read(a[i].l); read(a[i].r);
}
sort(a + 1, a + m + 1);
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 1; i <= n; ++i)
q.push(-1e9);
for (int i = 1; i <= m; ++i) {
int l = a[i].l - (int)(u * (double)(a[i].r - a[i].l + 1));
int t = q.top();
if (t >= l) continue;
q.pop();
ans[++cnt] = a[i].r;
q.push(a[i].r);
}
int T;
read(T);
while (T--) {
int x;
read(x);
if (x >= ans[cnt]) {
printf("%d\n", cnt);
continue;
}
int k = lower_bound(ans + 1, ans + cnt + 1, x + 1) - ans;
printf("%d\n", k - 1);
}
return 0;
}