题目:
hdu4864
题意:
给你n个任务和m个机器,每个机器时间和等级都 ≥ 一个任务的时间和等级就可以做这个任务并产生(任务时间 * 500 + 任务等级 * 2)的收益,而且每个机器最多只能做一个任务。求产生最多的收益。
思路:
因为时间的权值(500)比等级的权值(2)大,所有任务按时间从大到小排序,时间相同按等级从大到小排序。
机器按时间从小到大排序,时间相同按等级从小到大排序(也可以按时间从大到小排序,时间相同按等级从大到小排序)。
用一个数组记录每个等级可以完成这个任务的机器数量(ps:等级最大是100)
遍历每个任务,记录有多少个可以完成的机器的等级的数量,然后找最小等级的机器完成这个任务(如果用二分查找找到最小等级的机器则会TLE(两个 for 循环))。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXM = 110;
struct node{
int t, rank;
bool operator < (const node &a) const{
return (t == a.t) ? rank > a.rank : t > a.t;
}
}task[MAXN];
struct node1{
int t, rank;
bool operator < (const node1 &a) const{
return (t == a.t) ? rank < a.rank : t < a.t;
}
}ma[MAXN];
int Rank[MAXM];
int main(){
// freopen("_in.txt", "r", stdin);
// freopen("_out1.txt", "w", stdout);
int n, m, cnt, idx;
ll ans;
while (~scanf("%d%d", &m, &n)){
memset(Rank, 0, sizeof(Rank));
for (int i = 0; i < m; i++)
scanf("%d%d", &ma[i].t, &ma[i].rank);
for (int i = 0; i < n; i++){
scanf("%d%d", &task[i].t, &task[i].rank);
}
sort(ma, ma+m);
sort(task, task+n);
idx = m - 1;
cnt = 0;
ans = 0;
for (int i = 0; i < n; i++){
while (idx >= 0 && ma[idx].t >= task[i].t){ //记录机器的等级 >= 任务的等级
Rank[ma[idx].rank]++;
idx--;
}
for (int j = task[i].rank; j <= 100; j++)
if (Rank[j] != 0){ //找到了
Rank[j]--;
cnt++;
ans = ans + task[i].t * 500 + (task[i].rank << 1);
break;
}
}
printf("%d %lld\n", cnt, ans);
}
return 0;
}