题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4864
分析
贪心,观察数据规模可知,运行时间的优先级高于难度水平。
将机器和任务以此排序,完成一个任务只会对答案贡献一次,因此从收益更高的任务开始尽量完成。
每次为任务选择机器时,选在花费时间满足要求的前提下,难度水平尽可能小的,这样不会更差。
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
const int maxn = 1e5 + 5, maxy = 105;
struct MT {
int x, y;
bool operator < (const MT& rhs) const {
if (x == rhs.x) return y > rhs.y;
return x > rhs.x;
}
} m[maxn], t[maxn];
int a, b, y[maxy];
int main() {
while (scanf("%d%d", &a, &b) == 2) {
memset(y, 0, sizeof(y));
for (int i = 1; i <= a; ++i) m[i].x = read(), m[i].y = read();
for (int i = 1; i <= b; ++i) t[i].x = read(), t[i].y = read();
sort(m + 1, m + a + 1), sort(t + 1, t + b + 1);
int cnt = 0, j = 1;
long long money = 0;
for (int i = 1; i <= b; ++i) {
while (j <= a && m[j].x >= t[i].x) ++y[m[j++].y];
for (int k = t[i].y; k <= 100; ++k)
if (y[k]) {
++cnt, money += 500 * t[i].x + 2 * t[i].y;
--y[k];
break;
}
}
printf("%d %lld\n", cnt, money);
}
return 0;
}